plot - How can I remove spacing in Pie charts in multi panel and have only one legend at the top using r -
the pie charts:
the link data: https://drive.google.com/file/d/0bwopt0jygdzorkm3cva0wjjodvk/view?usp=sharing
mydata<-read.csv(file="final_analysis_candy_analysis.csv",head=true,sep=",") dhfr.arg <- table(mydata$dhfr.arg.59.163.137.) dhfr.ile <- table(mydata$dhfr.ile.51.214.65.) dhfr.asn108 <- table(mydata$dhfr.asn108.328.372.) glu.540 <- table(mydata$glu.540.538.326.200.) gly.437 <- table(mydata$gly.437.848.300.) library(plotrix) par(op) op <-par(mfrow=c(2,3),mar=c(0,0,1,0)) pct <- round(dhfr.arg/sum(dhfr.arg)*100) lbls <- paste(names(dhfr.arg), pct) # add percents labels lbls <- paste(lbls,"%",sep="") # ad % labels lp<-pie3d(dhfr.arg,radius=0.8,labels=lbls,explode=0.1, labelrad=1.4,main="dhfr arg 59(163,137)") pct <- round(dhfr.ile/sum(dhfr.ile)*100) lbls <- paste(names(dhfr.ile), pct) # add percents labels lbls <- paste(lbls,"%",sep="") # ad % labels lp<-pie3d(dhfr.ile,radius=0.8,labels=lbls,explode=0.1, labelrad=1.4,main="dhfr ile 51(214,65)") pct <- round(dhfr.asn108/sum(dhfr.asn108)*100) lbls <- paste(names(dhfr.asn108), pct) # add percents labels lbls <- paste(lbls,"%",sep="") # ad % labels lp <- pie3d(dhfr.asn108,radius=0.8,labels=lbls,explode=0.1, labelrad=1.4,main="dhfr asn108(328,372)") pct <- round(glu.540/sum(glu.540)*100) lbls <- paste(names(glu.540), pct) # add percents labels lbls <- paste(lbls,"%",sep="") # ad % labels lp<-pie3d(glu.540,radius=0.8,labels=lbls,explode=0.1, labelrad=1.4,main="glu 540(538,326,200)") pct <- round(gly.437/sum(gly.437)*100) lbls <- paste(names(gly.437), pct) # add percents labels lbls <- paste(lbls,"%",sep="") # ad % labels lp <- pie3d(gly.437,radius=0.8,labels=lbls,explode=0.1, labelrad=1.4,main="gly 437(848,300)") par(op)
using 2d visualization plot easier comprehend. therefore, alternative 2d solution ggplot
:
# load needed packages library(data.table) library(ggplot2) library(scales) # process & summarise data (with data.table) mydat <- melt(setdt(mydata), id=1, measure.vars=4:8)[, .n, = .(variable,value) ][, `:=` (perc = round(n/sum(n),2), pos = cumsum(n)-0.5*n), = variable] # create plot ggplot2 & scales ggplot(mydat) + geom_bar(stat="identity", aes(x="", y=n, fill=value)) + geom_text(aes(x = "", y = pos, label = percent(perc))) + scale_x_discrete(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) + coord_polar(theta = "y") + facet_grid(.~ variable, scales = "free") + theme_minimal(base_size = 16) + theme(axis.title = element_blank(), axis.text = element_blank(), panel.grid = element_blank(), legend.title = element_blank())
which gives following plot:
see this answer how calculate pos
variable base r or plyr , dplyr packages.
however, pie-charts not best way visualize data. in case bart chart result in clearer visualization. with:
ggplot(mydat, aes(x=variable, y=perc, fill=value)) + geom_bar(stat="identity", aes(label = percent(perc)), width=0.6) + scale_y_continuous(labels = percent(c(0,0.25,0.50,0.75,1.00))) + coord_flip() + theme_minimal(base_size = 14) + theme(axis.title = element_blank(), legend.title = element_blank())
you get:
Comments
Post a Comment