####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
大数据 | 数据挖掘 | R语言 R绘图Session#2 - Bar Plot
本文详细介绍了如何使用R语言进行条形图的绘制,包括直方图、分组条形图、堆叠条形图和比例条形图等。通过`ggplot2`库,展示了设置条形颜色、添加标签、调整宽度以及处理负数等多种技巧,帮助读者掌握R语言中的数据可视化方法。
摘要由CSDN通过智能技术生成