简体   繁体   English

无法在R中绘制同心饼图

[英]Can't draw a concentric pie chart in R

I have the following data:我有以下数据:

Phyla       V4         Fl
 <chr>     <dbl>      <dbl>

Proteobacteria  88.58    81.43  
Firmicutes  7.33    15.34   
Actinobacteriota    1.55    1.94        
Bacteroidota    2.20    1.25    

I want to display the data using a concentric pie chart.我想使用同心饼图显示数据。 I have a couple of trials:我有几个试验:

mycols <- c("#eee0b1", "#da8a67", "#e63e62", "#0033aa")
ggplot(df, aes(x = 2, y = V4, fill = Phyla)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0)+
  geom_text(aes(y = Fl, label = V4), color = "white")+
  scale_y_continuous(breaks=min(df$Fl):max(df$Fl)) +
  scale_fill_manual(values = mycols) +
  theme_void()+
  xlim(0.5, 2.5)

This generates这会产生在此处输入图像描述

So, I got only one column displayed.所以,我只显示了一列。

The other trial used this:另一个试验使用了这个:

pie(x=c(88.58,7.33,1.55,2.2),labels="",
col=c("#eee0b1", "#da8a67", "#e63e62", "#0033aa"))
par(new=TRUE)
pie(x=c(81.43,15.34,1.94, 1.25),labels=c("Proteobacteria","Firmicutes","Actinobacteriota", "Bacteroidota"),radius=.5,
    col=c("#eee0b1", "#da8a67", "#e63e62", "#0033aa"))

that generates this figure:生成这个数字:在此处输入图像描述

I do not know which is easier to fix to generate the concentric pie.我不知道哪个更容易生成同心饼图。 I need to include the color legend and label each pie with the category name (V4, Fl) along with adding the values as percentages.我需要包含颜色图例并用类别名称(V4,Fl)标记每个饼图,并将值添加为百分比。

You may try this你可以试试这个

df %>%
    pivot_longer(-Phyla, names_to = "type", values_to = "y") %>%
    ggplot(aes(x = type, y = y)) +
    geom_bar(aes(fill = Phyla), stat = "identity",
      color = "white", position = "fill", width=0.7) +
    coord_polar(theta = "y", start = pi/2) +
    geom_text(aes(y = y, group = Phyla, label = y),
      color = "white", position = position_fill(vjust=0.5)) +
    geom_text(aes(x = x, y = y, label = type),
      data = data.frame(x = c(2.5, 3.5), y = c(0, 0), type = c("V4", "Fl"))
    ) +
    scale_fill_manual(values = mycols) +
    scale_x_discrete(limits = c(NA, "V4", "Fl")) +
    theme_void()
  • pivot_longer transforms your data from "wide" to "long", so that you can draw multiple columns. pivot_longer将您的数据从“宽”转换为“长”,以便您可以绘制多列。
  • position="fill" in geom_bar() and position_fill in geom_text() will scale y value into [0,1], so that two columns are aligned. geom_bar() 中的position="fill"geom_text() geom_bar()中的position_fill会将 y 值缩放为 [0,1],以便两列对齐。
  • vjust=0.5 in position_fill will display values to their corresponding areas. position_fill中的vjust=0.5将显示相应区域的值。
  • It is a little difficult to label the circle directly using x axis texts, but you can label them manually using geom_text() with a new data.frame(x=c(2.5,3.5),y=c(0,0),type=c("V4","Fl"))直接使用 x 轴文本标记圆圈有点困难,但您可以使用geom_text()手动标记它们,并使用新的data.frame(x=c(2.5,3.5),y=c(0,0),type=c("V4","Fl"))

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM