0
votes

EDITED

I have the following example where I create 3 pie charts , but I would like to have them 3 combined into 1 pie + donuts pie. Besides, it would be really useful to have the numbers as well, how can this be accomplished? Thanks a lot.

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

This produces the following image: test

However, when I try to having the 3 of them together, the best I get is this mess: mess

How can I combine the pie charts above? And include numbers.

1

1 Answers

1
votes

This should get you started:

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

EDIT: Part of the reason, you get something different with faceting than without is because you use scales="free_y". To get the same thing without the facets, you can do scale the variables yourself.

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

However, your faceted plots also don't line up as nicely as your previous test data did. That just appears to be because the data is actually not exactly lined up (there are really only 2 unique values for the HLA.A11 and HLA.A24, so it's impossible to get 4 different sizes).