【问题标题】:Multiple ggplot pie charts with whole pies多个带有整个饼图的 ggplot 饼图
【发布时间】:2017-02-27 15:16:56
【问题描述】:

我想使用ggplot2 制作两个并排的饼图,但很难将两个饼图都制作成“整体” 这是我的数据示例。

> test
  New York Berlin         group
1      474    755 Never Visited
2      214    123  Visited Once
3       66    122   Visited > 1
4      142     64       Resided

当我尝试时:

  pie <- ggplot(data = melted2, aes(x = "", y = Cnt, fill = Type )) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  facet_grid(facets=. ~ City)  +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank()) + theme(legend.position='bottom') + guides(fill=guide_legend(nrow=2,byrow=TRUE))

pie

但这会产生:

编辑: Changing facet_grid(facets=. ~ City)facet_grid(City ~ ., scales = "free") 有效,但它会生成这样的垂直堆叠图表:

关于如何制作两个水平的完整饼图有什么建议吗?

这是数据:

> dput(melted2)
structure(list(Type = structure(c(1L, 4L, 3L, 2L, 1L, 4L, 3L, 
2L), .Label = c("Never Visited", "Resided", "Visited > 1", "Visited Once"
), class = "factor"), City = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L), .Label = c("New York", "Berlin"), class = "factor"), 
    Cnt = c(474L, 214L, 66L, 142L, 755L, 123L, 122L, 64L)), row.names = c(NA, 
-8L), .Names = c("Type", "City", "Cnt"), class = "data.frame")

【问题讨论】:

  • 也许你想要position_fill而不是position_stack,如图所示here
  • position_stack 将标签放在正确的位置,这是行不通的
  • 你试过了吗?因为我发现position_fill 把文字放在正确的位置相当不错。
  • 我已经尝试过了,但如果您认为您有解决方案,请提供可重现的答案。我提供了问题中的所有数据
  • 一个有效的方法是facet_grid(City ~ ., scales = "free") 但是这会产生垂直堆叠的饼图,而我需要水平的饼图

标签: r ggplot2


【解决方案1】:

要显示每个方面的相对比例,一种选择是使用position_fill。它适用于条形图和文本堆叠。

ggplot(data = melted2, aes(x = "", y = Cnt, fill = Type )) + 
    geom_bar(stat = "identity", position = position_fill()) +
    geom_text(aes(label = Cnt), position = position_fill(vjust = 0.5)) +
    coord_polar(theta = "y") +
    facet_wrap(~ City)  +
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank()) + 
    theme(legend.position='bottom') + 
    guides(fill=guide_legend(nrow=2, byrow=TRUE))

【讨论】:

  • 这可能是最好的选择。也许添加scale_y_continuous(labels = scales::percent)。我看到的唯一其他选择是放弃刻面并合并单独的图。
【解决方案2】:

如果您将比例输入ggplot2,它会起作用:

library(dplyr); library(ggplot2)
melted2 <- melted2 %>% group_by(City) %>% mutate(per = Cnt/sum(Cnt))
pie <- ggplot(data = melted2, aes(x = "", y = per, fill = Type)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  facet_grid(facets=. ~ City)  +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank()) + theme(legend.position='bottom') + guides(fill=guide_legend(nrow=2,byrow=TRUE))

pie

【讨论】:

    【解决方案3】:

    也许这就是您正在寻找的(显示百分比而不是计数):

    library(tidyverse)
    melted3 <- melted2 %>% group_by(City) %>% mutate(Percent = Cnt / sum(Cnt))
    
    pie <- ggplot(data = melted3, aes(x = "", y = Percent, fill = Type)) + 
      geom_bar(stat = "identity") +
      geom_text(aes(label = round(Percent, digits = 2)), position = position_stack(vjust = 0.5)) +
      coord_polar(theta = "y") +
      facet_grid(facets = . ~ City)  +
      theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) + theme(legend.position = 'bottom') +     guides(fill = guide_legend(nrow = 2, byrow = TRUE))
    

    【讨论】:

    • 不要对百分比本身进行四舍五入,而是对标签进行四舍五入(或者让 ggplot 使用 position_fill 处理)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2019-02-13
    相关资源
    最近更新 更多