2014-12-11 151 views
0

EDITEDR:联合饼图与GGPLOT2

我有以下例子,其中创建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() 

这将产生以下图片: test

然而,当我试图让他们的3一起,我得到的最好的是这个烂摊子: mess

我怎么能结合上面的饼图?并包括数字。

回答

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) 

enter image description here

编辑:部分原因,你得到的东西用不同刻面比不就是因为你用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种不同的大小)。

+0

非常感谢@shadow,这真的很有用!然而,我似乎并没有把我的头脑放在实际的数据上,我编辑了这个问题来反映它。希望你能帮助,谢谢! – DaniCee 2014-12-12 06:42:26