2014-12-11 150 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