用ggplots制作华夫饼图

时间:2019-11-10 12:15:54

标签: r ggplot2 waffle-chart

我正在尝试使用R从Tableau重现一个简单的华夫格图: enter image description here

我不确定waffle包中是否可行,但是,我非常感谢在两种情况下提供的帮助:

  • 有没有办法按行填色华夫饼图的颜色(例如 Tableau示例)不是按列的?
  • 是否可以在华夫饼图中添加标签百分比(24%)?

到目前为止,这是我的尝试:

 library(waffle)  

 # dummy sample 
 basedata <- c('User'=24, 'Not User'=  76)

 # Waffle chart
 waffle(
   basedata,
   rows = 10 ,
   colors =  c("#636363", "#fee8c8"),
   xlab = "1 square == 1%"
 ) +
   ggtitle("Some tilte") +
   theme(
     plot.title = element_text(hjust = 0.5, size = 27, face = "bold"),
     legend.text = element_text(size = 15),
     legend.position = "bottom"
   )

哪个会退回 enter image description here

1 个答案:

答案 0 :(得分:2)

这应该做到:

# dummy sample 
basedata <- c('User'=24, 'Not User'=  76)

# Waffle chart
waffle(
  basedata,
  rows = 10 ,
  colors =  c("#636363", "#fee8c8"),
  xlab = "1 square == 1%",
  flip = TRUE
) +
  ggtitle("Some tilte") +
  theme(
    plot.title = element_text(hjust = 0.5, size = 27, face = "bold"),
    legend.text = element_text(size = 15),
    legend.position = "bottom"
  ) +
  annotate("text", x = 4, y = 5, label = paste(basedata[1], "%"))

enter image description here

相关问题