【问题标题】:R: Combine pie charts with ggplot2R:将饼图与 ggplot2 结合起来
【发布时间】:2014-12-11 05:11:19
【问题描述】:

已编辑

我有以下示例,其中我创建了 3 个饼图,但我希望将它们 3 组合成 1 个饼图 + 甜甜圈饼图。 此外,还有数字真的很有用,如何做到这一点?非常感谢。

df.mut <- data.frame(Avrg.muts.HLA.A11.A24=c(20.20000,37.39286,11.85714,50.26087,20.20000,37.39286,11.85714,50.26087,20.20000,37.39286,11.85714,50.26087), Avrg.muts.HLA.A11=c(32.86842,32.86842,35.72973,35.72973,32.86842,32.86842,35.72973,35.72973,32.86842,32.86842,35.72973,35.72973), Avrg.muts.HLA.A24=c(15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608), variable=c("HLA.A11.A24","HLA.A11.A24","HLA.A11.A24","HLA.A11.A24","HLA.A11","HLA.A11","HLA.A11","HLA.A11","HLA.A24","HLA.A24","HLA.A24","HLA.A24"), value=c("+/+","+/-","-/+","-/-","+","+","-","-","+","-","+","-"))
df.mut$variable <- factor(df.mut$variable, levels=unique(df.mut$variable))
png(file="IMAGES/test1.png")
print(
  ggplot(df.mut, aes(x="")) +
    facet_grid(variable~., scales="free_y") +
    geom_bar(data=subset(df.mut, variable=='HLA.A11.A24'),
      aes(x='0', y=Avrg.muts.HLA.A11.A24, fill=value), width = 1, stat = "identity") +
    geom_bar(data=subset(df.mut, variable=='HLA.A11'),
      aes(x='1', y=Avrg.muts.HLA.A11, fill=value), width = 1, stat = "identity") +
    geom_bar(data=subset(df.mut, variable=='HLA.A24'),
      aes(x='2', y=Avrg.muts.HLA.A24, fill=value), width = 1, stat = "identity") +
    ggtitle("TEST1") +
    theme(axis.text.x=element_blank(), legend.title=element_blank(), legend.position="right", legend.background=element_blank(), legend.box.just="left", plot.title=element_text(size=15, face="bold", colour="black", vjust=1.5)) +
    scale_y_continuous(name="") +
    scale_x_discrete(name="") +
    coord_polar(theta="y")
)
dev.off()

这会产生以下图像:

但是,当我尝试将它们中的 3 个放在一起时,我得到的最好的结果就是这个烂摊子:

如何组合上面的饼图?并包括数字。

【问题讨论】:

    标签: r ggplot2 pie-chart


    【解决方案1】:

    这应该让你开始:

    df.test <- data.frame(genotype.1=c("+","+","-","-"), genotype.2=c("+","-","+","-"), count=c(345,547,678,987))  
    require(ggplot2)
    require(grid)
    
    ggplot(df.test, aes(y = count)) +
      geom_bar(aes(x='0', fill = paste(genotype.1, genotype.2, sep="/")), color='black', width = 1, stat = "identity") +
      geom_bar(aes(x='1', fill = genotype.1), width = 1, color='black', stat = "identity") +
      geom_bar(aes(x='2', fill = genotype.2), width = 1, color='black', stat = "identity") +
      coord_polar(theta="y") + 
      scale_x_discrete(name='', breaks=c('0', '1', '2'), labels=rep('', 3)) + 
      theme(axis.ticks.length = unit(0, "npc")) + 
      scale_fill_discrete(name='genotype', breaks = c('-', '+', '-/-', '-/+', '+/-', '+/+')) +
      scale_y_continuous(breaks=0)
    

    编辑:部分原因是你使用了scales="free_y"。要在没有方面的情况下获得相同的结果,您可以自己缩放变量。

    p <- ggplot(df.mut, aes(x="")) +
      geom_bar(data=subset(df.mut, variable=='HLA.A11.A24'),
               aes(x='0', y=Avrg.muts.HLA.A11.A24/sum(Avrg.muts.HLA.A11.A24), fill=value), color='black', width = 1, stat = "identity") +
      geom_bar(data=subset(df.mut, variable=='HLA.A11'),
               aes(x='1', y=Avrg.muts.HLA.A11/sum(Avrg.muts.HLA.A11), fill=value), color='black', width = 1, stat = "identity") +
      geom_bar(data=subset(df.mut, variable=='HLA.A24'),
               aes(x='2', y=Avrg.muts.HLA.A24/sum(Avrg.muts.HLA.A24), fill=value), color='black', width = 1, stat = "identity") +
      ggtitle("TEST1") +
      theme(axis.text.x=element_blank(), legend.title=element_blank(), legend.position="right", legend.background=element_blank(), legend.box.just="left", plot.title=element_text(size=15, face="bold", colour="black", vjust=1.5)) +
      scale_y_continuous(name="") +
      scale_x_discrete(name="") +
      coord_polar(theta="y")
    # now look at the faceted and unfaceted plots...
    p
    p + facet_grid(variable~., scales="free_y") 
    

    但是,您的多面图也没有像之前的测试数据那样排列整齐。这似乎是因为数据实际上并没有完全对齐(HLA.A11 和 HLA.A24 实际上只有 2 个唯一值,因此不可能获得 4 个不同的大小)。

    【讨论】:

    • 非常感谢@shadow,这真的很有用!但是,我似乎并没有完全了解我拥有的实际数据,我已经编辑了问题以反映它。希望你能帮忙,谢谢!
    猜你喜欢
    • 2019-12-03
    • 2020-11-09
    • 2020-10-19
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多