使用ggplot绘制直方图中条形的观察数

时间:2015-01-22 14:55:42

标签: r plot ggplot2 histogram

我想在直方图的上方添加每个变量的观察数量。这是我的数据帧。

means <- structure(list(Driver = c("Crop agriculture", "Crop agriculture", 
"Infrastructure", "Infrastructure", "Mining", "Mining", "Mixed Agriculture", 
"Mixed Agriculture", "Other land use", "Other land use", "Pasture", 
"Pasture", "Tree crops", "Tree crops", "Water", "Water"), Period = c("1990-2000", 
"2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005", 
"1990-2000", "2000-2005", "1990-2000", "2000-2005", "1990-2000", 
"2000-2005", "1990-2000", "2000-2005", "1990-2000", "2000-2005"
), mean = c(36.2697273704497, 61.3311804191792, 28.7523209391483, 
30.955220240622, 49.4900570536558, 41.5037095947389, 13.1454310847706, 
10.3642833385884, 28.5871996967629, 23.3988064930454, 53.9768942854543, 
61.3460606144189, 17.3260123546446, 16.1278503954073, 36.6165841628849, 
32.6896740558384), n = c("n = 1669", "n = 783", "n = 298", "n = 151", 
"n = 20", "n = 7", "n = 1355", "n = 925", "n = 1623", "n = 851", 
"n = 10986", "n = 6039", "n = 316", "n = 211", "n = 466", "n = 244"
)), .Names = c("Driver", "Period", "mean", "n"), class = "data.frame", row.names = c(NA, 
-16L))

基于此数据框,这是我想要绘制的内容。

means.barplot <- ggplot(means, aes(x = Driver, y = mean, fill = Period, 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, 70)) + 
theme_classic(base_size = 20, base_family = "") + 
theme(panel.grid.minor = element_line(colour =" grey", size = 0.5))

但是,我还想在这个图中添加每个变量的观察数(在这种情况下每个周期的驱动程序)。我还没设法做到这一点。有人可以帮我解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:2)

一种方法是使用ggplot_build()annotate()。您希望通过创建一次ggplot对象来获取x和y位置的值。这里的对象是g。使用ggplot_build(objectname)$data[[1]]时,您可以获得所需的值。在annotate()中,您可以使用foo中的x和y值以及所需的标签(即mydf $ n)。另外,我添加了theme(axis.text.x = element_text(angle = 45, hjust = 1))来修改x轴标签。

g <- ggplot(mydf, aes(x = Driver, y = mean, fill = Period, 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,70)) +
     theme_classic(base_size = 20, base_family = "") + 
     theme(panel.grid.minor = element_line(colour = "grey", size = 0.5)) +
     theme(axis.text.x = element_text(angle = 45, hjust = 1))


### Create a data frame for annotate()
foo <- ggplot_build(g)$data[[1]]

g + annotate("text", x = foo$x, y = foo$y + 1, label = mydf$n, size = 3)

enter image description here

相关问题