使用 ggplot 在直方图中绘制条形观测值

Plot number of observations of bars in a histogram using ggplot

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

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))

但是,我还想在此图中添加每个变量的观察次数(在本例中为每个时期的驱动因素)。我还没有设法做到这一点。有人可以帮我解决这个问题吗?谢谢。

一种方法是使用 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)