【问题标题】:Add text on top of a faceted dodged bar chart使用 ggplot2 在分面闪避的条形图顶部添加文本
【发布时间】:2014-12-26 22:31:25
【问题描述】:

我想为两个不同年份绘制一个躲避的条形图,并将收入数字相应地放在条形图的顶部。尝试了我在这里找到的一些建议后,我仍然无法完全得到我想要的(所有数字都显示在中间条/列的中间,而不是平均分布)。任何建议将不胜感激。谢谢!

我最近的尝试

# Disable scientific notation
options("scipen" = 100, "digits" = 1)

censusData <- structure(list(Year = c(2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 
                                      2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 
                                      2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 2007L, 2012L, 2007L
                                      ), County = c("A", "A", "B", "B", "C", "C", "Sum", "Sum", "A", 
                                      "A", "B", "B", "C", "C", "Sum", "Sum", "A", "A", "B", "B", "C", 
                                      "C", "Sum", "Sum"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 
                                      1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
                                      3L, 3L, 3L), .Label = c("Total_Revenue", "Crop_Revenue", "Livestock_Revenue"
                                      ), class = "factor"), value = c(1645.51, 1203.806, 68.911, 60.949, 
                                      923.163, 525.918, 2637.584, 1790.673, 1069.497, 787.459, 47.157, 
                                      38.735, 825.050228, 470.024, 1941.704228, 1296.218, 576.013, 
                                      416.347, 21.754, 22.214, 98.112772, 55.894, 695.879772, 494.455
                                      )), row.names = c(NA, -24L), .Names = c("Year", "County", "variable", 
                                      "value"), class = "data.frame")

# Dodged barplot
qbarplot_yr_1 <- ggplot(censusData, aes(County, value)) + facet_grid(. ~ Year) +  
  geom_bar(aes(fill = variable), position = "dodge", stat="identity") +
  xlab("County") + ylab("Revenue (Million USD)") +
  scale_fill_discrete(name = 'Legend', labels=c("Total", "Crop", "Livestocks")) +
  theme(axis.ticks.x = element_blank()) +
  theme(panel.background = element_rect(colour='dark grey')) +
  theme(strip.text.x = element_text(size = 20, face="bold"),
        strip.background = element_rect(colour="dark grey"))

# Add text on top of the bar
qbarplot_yr_1 + geom_text(data = censusData,
                          aes(x = County, y = value + 150, label = format(value, nsmall = 0, scientific = FALSE)), 
                          color="blue")

【问题讨论】:

    标签: r text ggplot2 bar-chart facet


    【解决方案1】:

    您还需要避开文本值。试试

    qbarplot_yr_1 + geom_text(data = censusData,
             aes(x = County, group=variable, y = value + 150, 
             label = format(value, nsmall = 0, digits=1, scientific = FALSE)), 
             color="blue", position=position_dodge(.9), hjust=.5)
    

    我们还需要group=,以便它知道要避开哪些值。

    【讨论】:

    • 谢谢。不知道为什么我忽略了在我的 geom_text() 电话中包含 group 但这救了我!
    【解决方案2】:

    使用ggfittex 包添加另一个解决方案

    library(ggplot2)
    library(ggfittext)
    
    ggplot(censusData, 
           aes(County, value,
               fill = variable,
               label = format(value, 
                              nsmall = 0, 
                              big.mark   = ",",
                              scientific = FALSE))) + 
      facet_grid(. ~ Year) +  
      geom_col(position = "dodge") +
    
      # from `ggfittext`
      geom_bar_text(position = "dodge") +
    
      scale_fill_discrete(name = 'Legend', labels = c("Total", "Crop", "Livestocks")) +
      theme_minimal(base_size = 14) +
      theme(axis.text.y = element_blank(),
            strip.text = element_text(face = 'bold'))
    

    reprex package (v0.3.0) 于 2021-01-20 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      相关资源
      最近更新 更多