我有一个示例数据框,如下所示:
set.seed(87)
df = data.frame(
sample = rep(c("PB","PB","BM","BM"),8),
status = rep(c("healthy","myeloma"),16),
family = c(rep("CD4",16),rep("CD8",16)),
phenotype = rep(c("Tn","Tn","Tn","Tn","Tcm","Tcm","Tcm","Tcm","Tem","Tem","Tem","Tem","Temra","Temra","Temra","Temra"),2),
percent = sample(20:30,32,replace=T)
)我想以饼形图的形式绘制数据,同时在x轴和y轴上进行分面,格式类似于我在网上找到的这个漂亮的图表:

然而,我只知道如何通过首先将两个变量(CD4/CD8和healthy/myeloma)分组在一起来进行刻面,下面的代码不能很好地将x轴变量分成两行:
df %>%
mutate(group = paste(family,status)) %>%
ggplot(aes(x = "",y=percent,fill = phenotype)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
facet_grid(sample~group) +
theme(axis.text.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank())

感谢任何关于如何让整个图表看起来更像在线图表的想法,谢谢!
发布于 2021-08-10 03:59:34
也许这里更近一些-
library(dplyr)
library(ggplot2)
df %>%
mutate(group = paste(family,status)) %>%
ggplot(aes(x = "",y=percent,fill = phenotype)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
facet_grid(sample~group) +
theme_classic() +
theme(axis.text.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
strip.background = element_blank(),
strip.text = element_text(size = 15, face = 'bold'),
legend.position="bottom")

https://stackoverflow.com/questions/68720793
复制相似问题