ggplot2绘制条形图——bar plot using ggplot2

      在实际应用过程中,学会了用ggplot2绘制条形图。也许是最基本的用法,但是在此记录备忘。
      条形图一般用于值大小的比较,或是值之间是否有显著性差异。不涉及值之间的趋势,也不涉及值之间的相关等。
      首先是数据格式。可以定义的有:类别(group)、X轴对象(subject / individual)、对象的分组(facet)、Y轴值(value)、标准差或标准误(SD/SE)。数据表格大致如下:
      第一列是数据的类别,即想在条形图中显示条形的类别。
      第二列是对象/个体的标签名称,即显示在X轴上的对象名称。
      第三列、第四列是数据值,这里直接给出了平均值和标准差,有时需要根据现有数据计算出来。
      第五列是对象的分组,即将X轴的各个对象/个体自定义进行分组。



      下图显示了数据的对应结构:


      如何在R中绘制呢?

library(ggplot2)
## Input the data
hetm <- read.table("data.txt", head=TRUE# Replace the quotes with your own data path
## Plot the graph
p <- ggplot(hetm, aes(x=pop, y=mean, fill=het)) + geom_bar(stat="identity", color="black", position=position_dodge(), lwd=0) + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0, lwd=0.2, position=position_dodge(0.9))
## Define the axis, color, font size, et al.
p1 <- p + labs(y = "Value") +
scale_fill_manual("", values=c('#cecece','#005087')) + theme(panel.spacing.x = unit(0, "line"), axis.text.x = element_text(size = 14, angle = 60, vjust = 1, hjust=1), axis.text.y = element_text(size = 9), axis.title.y=element_text(size = 14), axis.title.x=element_blank(), legend.text = element_text(size = 14, face="italic"), panel.background = element_rect(fill="white"), panel.grid.major=element_line(size=0.1, colour="gray90"), panel.border=element_rect(colour="gray40", fill=NA), strip.background =element_rect(colour="gray40", fill=NA), strip.text.x=element_text(size=14, face="bold"), legend.position="top", legend.direction="horizontal", legend.margin = unit(0,"lines"), legend.justification=c(1,0)) + facet_grid(~ con, scales="free_x", space="free_x")
dev.new(width=9, height=2.88, unit="in")
## Print the graph
p1

      图像如下:
对象按照原始默认的字母顺序
      这里,你会发现每个X轴对象标签名称是按照首字母顺序排列的。也就是说,geom_bar()默认将对象按照默认顺序排序,然后输出图像。
      如果想要将对象顺序按照自己自定义的顺序,只需要在画plot之前重新排序对象即可。

hetm$pop <- factor(hetm$pop, levels=c("Aomori",
"Akita",
"Miyagi",
"Fukushima",
"Nagano",
"Kanagawa",
"Shizuoka",
"Nagoya",
"Shiga",
"Biwako",
"Nara",
"Osaka",
"Kagawa",
"Kochi",
"Yamaguchi",
"Kumamoto",
"Nagasaki",
"Kagoshima",
"TotalJapan",
"Changchun",
"Anhui",
"Nanjing",
"Hangzhou",
"ChunAn",
"Hubei",
"Jiujiang",
"Sichuan",
"Yangzonghai",
"Shilin",
"HongKong",
"Yilan",
"Tainan",
"TotalChina"))
## Here also reorder the facets
hetm$con <- factor(hetm$con, levels=c("Japan", "China"))
##########
## Plot the graph
px <- ggplot(hetm, aes(x=pop, y=mean, fill=het)) + geom_bar(stat="identity", color="black", position=position_dodge(), lwd=0) + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0, lwd=0.2, position=position_dodge(0.9))
## Define the axis, color, font size, et al.
px1 <- px + labs(y = "Value") +
scale_fill_manual("", values=c('#cecece','#005087')) + theme(panel.spacing.x = unit(0, "line"), axis.text.x = element_text(size = 14, angle = 60, vjust = 1, hjust=1), axis.text.y = element_text(size = 9), axis.title.y=element_text(size = 14), axis.title.x=element_blank(), legend.text = element_text(size = 14, face="italic"), panel.background = element_rect(fill="white"), panel.grid.major=element_line(size=0.1, colour="gray90"), panel.border=element_rect(colour="gray40", fill=NA), strip.background =element_rect(colour="gray40", fill=NA), strip.text.x=element_text(size=14, face="bold"), legend.position="top", legend.direction="horizontal", legend.margin = unit(0,"lines"), legend.justification=c(1,0)) + facet_grid(~ con, scales="free_x", space="free_x")
dev.new(width=9, height=2.88, unit="in")
## Print the graph
px1

      输出图像即达到自定义的要求:
对象顺序按照自定义的重新排序
      这时,就得到了最终想要的结果。
      修改条形宽度、条形的外边框、误差线的型式,可参考geom_bar()和geom_errorbar()中的各项参数。
      修改轴、轴字体、轴标题及字体、图片框背景、图片框网格线型式、条形填充颜色、标签类别字体、标签位置及方向、对象分组的字体等等,都可以使用theme()来完成。详见函数theme()。
改变标签颜色等


      ~~图像似乎也是有生命的~~
      ~~ R is Amazing! ~~

评论

此博客中的热门博文

带误差条的线图 / Line plot with error bar in R

R数据分类汇总 / Data Classification in R