【问题标题】:Formatting facet pie chart ggplot格式化分面饼图ggplot
【发布时间】:2021-08-10 03:33:53
【问题描述】:

我有一个示例数据框如下:

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 和健康/骨髓瘤)分组在一起以及以下代码来分面,这不能让我很好地将 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())

感谢任何关于如何使整个图表看起来更像在线图表的想法,谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    也许这更接近 -

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多