[英]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. 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
将显示相应区域的值。geom_text()
with a new data.frame(x=c(2.5,3.5),y=c(0,0),type=c("V4","Fl"))
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.