【问题标题】:Can this type of plot be done with ggplot2?这种类型的情节可以用ggplot2完成吗?
【发布时间】:2018-08-30 10:58:14
【问题描述】:

我正在使用Rggplot2 做一些用于发布目的的绘图。我遇到了这个情节,我想使用ggplot2 复制它。但是,我从未见过使用ggplot2 制作的这样的情节。

可以用ggplot2完成吗?栏下面的文字呢?我想这些必须在ggplot2 代码中进行硬编码。以及如何对齐这些文本?

【问题讨论】:

标签: r ggplot2 bar-chart


【解决方案1】:

这已经相当接近了:

# Generate sample data (I'm too lazy to type out the full labels)
df <- data.frame(
    perc = c(60, 36, 44, 41, 42, 57, 34, 52),
    type = rep(c("blue", "green"), 4),
    label = rep(c(
        "Individual reports created as needed",
        "Regular reports on single topics",
        "Analytics using data integrated from multiple systems",
        "Business unit-specific dashboards and visuals"), each = 2))


library(ggplot2)
ggplot(df, aes(1, perc, fill = type)) +
    geom_col(position = "dodge2") +
    scale_fill_manual(values = c("turquoise4", "forestgreen"), guide = FALSE) +
    facet_wrap(~ label, ncol = 1, strip.position = "bottom") +
    geom_text(
        aes(y = 1, label = sprintf("%i%%", perc)),
        colour = "white",
        position = position_dodge(width = .9),
        hjust = 0,
        fontface = "bold") +
    coord_flip(expand = F) +
    theme_minimal() +
    theme(
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.text = element_text(angle = 0, hjust = 0, face = "bold"))

几个解释:

  1. 我们分别使用position = "dodge2"(请注意,这需要ggplot_ggplot2_3.0.0,否则使用position = position_dodge(width = 1.0))和position = position_dodge(width = 0.9),使用躲避的条形和匹配的躲避标签。
  2. 我们使用facet_wrap 并强制采用单列布局;条形标签移至底部。
  3. 我们使用 coord_flip(expand = F) 旋转整个绘图,其中 expand = F 确保左对齐 (hjust = 0) 刻面条文本与 0 对齐。
  4. 最后我们调整了主题以增加整体审美相似性。

【讨论】:

  • 很好的答案!
  • @user3115933 谢谢,这很有趣:-)
  • 我复制了您的确切代码以在 RStudio 中运行它,我收到以下错误消息:错误:找到的对象不是位置。有什么问题?
  • @user3115933 奇怪;你的ggplot 版本是什么?我在ggplot2_3.0.0 上试过这个,但我无法重现该错误。您必须浏览不同的绘图组件才能查看哪条线引发了错误。或者可能更简单,更新ggplot2
  • @user3115933 作为position = "dodge" 的替代方案,您还可以在geom_col 中使用position = position_dodge(width = 1)。我在回答中进行了编辑以澄清。
【解决方案2】:

您可以尝试使用其他答案中的数据。区别在于:我们使用scales::percent 来绘制百分比。我们使用ggpubr::theme_transparent() 主题来尽可能减少调整。

df$perc <- c(.60, .36, .44, .41, .42, .57, .34, .52)

ggplot(df, aes(label, perc, label=scales::percent(round(perc,2)),fill= factor(type))) + 
   geom_col(position = position_dodge(0.9), show.legend = F) + 
   geom_text(aes(y=0), position = position_dodge(0.9), size=5, hjust=-0.1, color="white", fontface="bold") +
   scale_y_continuous("",labels = scales::percent) + 
   coord_flip(expand = F) + 
   facet_wrap(~label,scales = "free", strip.position = "bottom", ncol = 1) +
   ggpubr::theme_transparent() +
   xlab("") + 
   theme(strip.background = element_blank(),
         strip.text = element_text(size = 12, face = "bold",hjust=0))

【讨论】:

    【解决方案3】:

    也许使用 facet wrap 并调整样式?

    dat <- data.frame(perc = c(60, 20, 90, 30), col = rep(c("gr1", "gr2"), 2),
                      text = c(rep("text1", 2), rep("text2", 2)))
    
    ggplot(dat, aes(y = perc, x = col, fill = col)) +
      geom_bar(stat = "identity", position = "dodge") +
      coord_flip() +
      facet_wrap(~text, strip.position = "bottom", ncol = 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多