可以使用ggplot2完成这种绘图吗?

时间:2018-08-30 10:58:15

标签: r ggplot2 bar-chart

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

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

Bar graph

3 个答案:

答案 0 :(得分:9)

这非常接近:

lsmod

enter image description here

一些解释:

  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")) 的躲避标签(请注意,这需要position = "dodge2",否则使用ggplot_ggplot2_3.0.0)和position = position_dodge(width = 1.0)
  2. 我们使用position = position_dodge(width = 0.9)并强制采用单列布局;条形标签被移到底部。
  3. 我们使用facet_wrap旋转整个图,其中coord_flip(expand = F)确保左对齐(expand = F)的小平面条文本与0对齐。
  4. 最后,我们调整主题以提高整体美学相似度。

答案 1 :(得分:4)

您可以尝试使用其他答案中的数据。不同之处在于:我们使用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))

enter image description here

答案 2 :(得分:0)

也许使用多面环绕和调整样式?

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)