R ggplot 分面饼图:整个饼图与部分饼图

R ggplot facetted pie chart: whole pies vs parts of pies

提问人:michel 提问时间:8/25/2023 更新时间:8/25/2023 访问量:33

问:

我想在 R/ggplot 中绘制一个分面饼图。有 3 个维度需要绘制。

我能够实现的情节通常是我想要的,除了一个细节: 使用 geom_bar(stat=“identity”,......,position=position_fill())+ 所有 3 个圆都是整圆 whole circles

with geom_bar(stat=“identity”, .....,position = position_stack())+ 一个圆是完整的,两个是圆的一部分 partly whole

我想要这个图的一个版本,其中所有的圆圈都是一个圆圈的一部分,代表它相对于其他圆圈的比例。我怎样才能实现第一个圆圈不是一个整体,而是代表它相对于其他圆圈的比例(A = 大约 70,B = 大约 20,C = 大约 10)?

这是我在可重现代码中尝试过的内容:

df<-structure(list(x = structure(c(11L, 10L, 9L, 8L, 7L, 6L, 5L, 
                                  4L, 3L, 2L, 1L, 17L, 16L, 15L, 14L, 13L, 12L, 20L, 19L, 18L), levels = c("x1", 
                                                                                                           "x2", "x3", "x4", "x5", 
                                                                                                           "x6", "x7", "x8", "x9", 
                                                                                                           "x10", "x11", "x12", 
                                                                                                           "x13", "x14", "x15", 
                                                                                                           "x16", "x17", 
                                                                                                           "x18", "x19", "x20"
                                  ), class = "factor"), prop = c(10.4723707664884, 8.33333333333333, 
                                                                         7.79857397504456, 7.70944741532977, 7.62032085561497, 7.39750445632799, 
                                                                         6.95187165775401, 5.88235294117647, 2.94117647058824, 2.76292335115865, 
                                                                         2.18360071301248, 4.23351158645276, 3.83244206773619, 3.6096256684492, 
                                                                         3.29768270944742, 2.89661319073084, 2.22816399286988, 4.76827094474153, 
                                                                         2.67379679144385, 2.40641711229947), dim = c("A", 
                                                                                                                                          "A", "A", "A", "A", "A", 
                                                                                                                                          "A", "A", "A", "A", "A", 
                                                                                                                                          "B", "B", "B", "B", "B", 
                                                                                                                                          "B", "C", "C", "C")), row.names = c(NA, 
                                                                                                                                                                                                          -20L), class = "data.frame")

colorramps<-c(paletteer_c("ggthemes::Green", 11),
                  paletteer_c("ggthemes::Classic Blue", 6),
                  paletteer_c("ggthemes::Purple", 3)
)

#whole circles
x11(25,12)
ggplot(df,aes(x="",y=prop, fill=x))+
  geom_bar(stat="identity", width=1,col="black",position=position_fill())+
  facet_grid(~dim)+
  coord_polar("y", start=0)+
  scale_fill_manual(values = colorramps) +
  geom_text(aes(label = round(as.numeric(prop),digits=0)),
            position = position_fill(vjust=0.5)) +
  labs(x = NULL, y = NULL, fill = NULL)+
  theme_classic()+
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())+
  labs(title = "Proportion of x by dimension")

#partly whole circle
x11(25,12)
ggplot(df,aes(x="",y=prop, fill=x))+
  geom_bar(stat="identity", width=1,col="black",position = position_stack())+
  facet_grid(~dim)+
  coord_polar("y", start=0)+
  scale_fill_manual(values = colorramps) +
  geom_text(aes(label = round(as.numeric(prop),digits=0)),
            position = position_stack(vjust=0.5)) +
  labs(x = NULL, y = NULL, fill = NULL)+
  theme_classic()+
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())+
  labs(title = "Proportion of x by dimension")
r ggplot2 饼图 分面网格 比例

评论


答:

1赞 stefan 8/25/2023 #1

您必须将比例设置为 0 到 100 的范围:limitsy

library(ggplot2)
library(paletteer)

colorramps <- c(
  paletteer_c("ggthemes::Green", 11),
  paletteer_c("ggthemes::Classic Blue", 6),
  paletteer_c("ggthemes::Purple", 3)
)

ggplot(df, aes(x = "", y = prop, fill = x)) +
  geom_bar(
    stat = "identity", width = 1, col = "black",
    position = position_stack()
  ) +
  facet_wrap(~dim) +
  scale_y_continuous(limits = c(0, 100)) +
  coord_polar("y", start = 0) +
  scale_fill_manual(values = colorramps) +
  geom_text(aes(label = round(as.numeric(prop), digits = 0)),
    position = position_stack(vjust = 0.5)
  ) +
  labs(x = NULL, y = NULL, fill = NULL) +
  theme_classic() +
  theme(
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  ) +
  labs(title = "Proportion of x by dimension")

enter image description here