【问题标题】:Plot number of observations of bars in a histogram using ggplot使用 ggplot 在直方图中绘制条形的观察数
【发布时间】:2015-01-22 14:55:41
【问题描述】:

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

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

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

【问题讨论】:

    标签: r plot ggplot2 histogram


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      相关资源
      最近更新 更多