在ggplot中绘制直方图

时间:2015-01-19 11:17:49

标签: r ggplot2 histogram

我有这个数据框。

dput(EF_Lat_Am)
structure(list(V1 = structure(c(4L, 3L, 5L, 6L, 1L, 2L, 7L, 8L, 
4L, 3L, 5L, 6L, 1L, 2L, 7L, 8L), .Label = c("Crop Agriculture", 
"Mining", "Mixed Agriculture", "Other land use", "Pasture", "Tree crops", 
"Urban", "Water"), class = "factor"), V2 = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Emission Factor", class = "factor"), 
    V3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2000", "2005"), class = "factor"), 
    V4 = c(77.0109486116396, 69.2454348145657, 73.684103657833, 
    71.0430911289891, 43.136201172115, 117.358146800995, 77.4653952935238, 
    89.0966064142874, 71.8286578413912, 67.9099357961953, 76.7438444998728, 
    67.4818461466729, 50.6468079101972, 117.799797611894, 78.7347377710757, 
    81.3020943196897)), .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA, 
16L), class = "data.frame")

正如您在2000年和2005年所看到的那样,我对每种土地利用都有排放因子值。我想绘制直方图,其中x轴为土地利用类型,y轴为排放因子。此外,对于每个土地使用,我希望两年的酒吧是相邻的。我还想要一个传奇,显示哪些年份与酒吧相对应(2000年或2005年)。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这是答案。

ggplot(EF_Lat_Am, aes(x=V1, y = V4, fill=V3, width=.85)) + geom_bar(position="dodge", stat="identity") +
  labs(x = "", y = "EF (T/ha)") +
  theme(axis.text=element_text(size=16),axis.title=element_text(size=20), 
        legend.title=element_text(size=20, face='bold'),legend.text=element_text(size=20), axis.line = element_line(colour = "black")) +
  scale_fill_grey("Period") + scale_y_continuous(limits=c(0,120)) + theme_classic(base_size = 20, base_family = "")