大数据 | 数据挖掘 | R语言 R绘图Session#2 - Bar Plot

####Bar Plot
#There’s an important distinction you should be aware of when making bar graphs: 
#sometimes the bar heights represent counts of cases in the data set, 
#and sometimes they represent values in the data set.

# x vector is factor, y vector is the height of the bar respectively
> install.packages("gcookbook")
> library(gcookbook) 
> ggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity")
# same to
> qplot(group, weight, data=pg_mean, geom="bar", stat="identity")

# add some fill color and outline
> ggplot(pg_mean, aes(x=group, y=weight)) +
    geom_bar(stat="identity", fill="lightblue", colour="black")
	
##Grouped bar: one categorical variable as X, the other continuous variable as Y; 
##If we want to split X with another categorical variable Z, then use FILL and position=dodge (if no dodge, it's an stacked bar)
##Then Bars with same X but different Z will be grouped together, as below example.
> ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
    geom_bar(position="dodge", stat="identity")
> ce <- cabbage_exp[1:5, ]   # Copy the data without last row
#the last bar will be missing
> ggplot(ce, aes(x=Date, y=Weight, fill=Cultivar)) +
    geom_bar(position="dodge", colour="black", stat="identity") +
    scale_fill_brewer(palette="Pastel1")

##a Bar Graph of Counts
> ggplot(diamonds, aes(x=cut)) + geom_bar()
# Equivalent to using geom_bar(stat="bin")

#if the x is categorical/discrete, then its a count bar
#if the x is continuous, then its a histogram, same as geom_histogram()
> ggplot(diamonds, aes(x=carat)) + geom_bar()

##colour
> upc <- subset(uspopc
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值