【问题标题】:Plotting multiple Pie Charts with label in one plot在一个图中绘制多个带有标签的饼图
【发布时间】:2019-02-13 14:47:58
【问题描述】:

前几天我遇到了这个问题,并试图为自己重新创建它。 ggplot, facet, piechart: placing text in the middle of pie chart slices 。我的数据格式非常相似,但遗憾的是接受的答案没有帮助,因此我重新发布。

我本质上想用我自己的数据创建公认的答案,但我遇到的问题是coord_polar does not support free scale。使用第一个答案:

我尝试使用答案的第二个版本和 ddplyr 版本,但我也没有得到我想要的输出。使用第二个答案:

显然,这些都没有预期的效果。我更愿意像大小饼图一样创建一个,但仅显示四个作为示例,如下所示:。 这是我在 excel 中做的,但有一个图例,没有背景网格。

代码

title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)

piec<-data.frame(title,type,value)
library(tidyverse)    

p1<-ggplot(data = piec, aes(x = "", y = value, fill = type)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = value), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") 
  #facet_grid(title ~ ., scales = "free")
p1


piec <- piec %>% group_by(title) %>% mutate(pos=cumsum(value)-0.5*value)
p2<-ggplot(data = piec) + 
  geom_bar(aes(x = "", y = value, fill = type), stat = "identity") +
  geom_text(aes(x = "", y = pos, label = value)) +
  coord_polar(theta = "y") 
  #facet_grid(Channel ~ ., scales = "free") 
p2

【问题讨论】:

    标签: r ggplot2 plot pie-chart


    【解决方案1】:

    您不必为 geom_textgeom_bar 提供不同的 y 值(两者都使用 y = value)。接下来,您必须在geom_text 中指定位置。最后,从构面中删除scales

    library(ggplot2)
    
    title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
    type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
    value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
    piec<-data.frame(title,type,value)
    
    ggplot(piec, aes("", value, fill = type)) + 
        geom_bar(stat = "identity", color = "white", size = 1) +
        geom_text(aes(label = paste0(value * 100, "%")), 
                  position = position_stack(vjust = 0.5), 
                  color = "white", size = 3) +
        coord_polar(theta = "y") +
        facet_wrap(~ title, ncol = 3) +
        scale_fill_manual(values = c("#0048cc", "#cc8400")) +
        theme_void()
    

    【讨论】:

    • 好的。我知道我没有指定这个,在这种情况下忘记它,但无论如何要删除饼图上的白线?什么都没有替换它们?
    • @JackArmstrong 在geom_bar 部分删除color = white
    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    相关资源
    最近更新 更多