Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove spacing in Pie charts in multi panel and have only one legend at the top using r

Tags:

plot

r

pie-chart

The pie charts:

Initial pie charts diagram

THE LINK TO 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 to labels    
lbls <- paste(lbls,"%",sep="") # ad % to 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 to labels    
lbls <- paste(lbls,"%",sep="") # ad % to 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 to labels    
lbls <- paste(lbls,"%",sep="") # ad % to 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 to labels   
lbls <- paste(lbls,"%",sep="") # ad % to 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 to labels    
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp <- pie3D(gly.437,radius=0.8,labels=lbls,explode=0.1,
  labelrad=1.4,main="gly 437(848,300)")
par(op)

pie charts with white space marked in black

like image 762
andy Avatar asked Oct 18 '22 16:10

andy


2 Answers

Using a 2D visualization will your plot much easier to comprehend. Therefore, an alternative 2D solution with ggplot:

# load needed packages
library(data.table)
library(ggplot2)
library(scales)

# process & summarise the data (with data.table)
mydat <- melt(setDT(Mydata),
              id=1,
              measure.vars=4:8)[, .N, by = .(variable,value)
                                ][, `:=` (perc = round(N/sum(N),2),
                                          pos = cumsum(N)-0.5*N), by = variable]

# create the plot with 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 the following plot:

enter image description here

See this answer for how to calculate the pos variable with base R or with the plyr and dplyr packages.


However, pie-charts are mostly not the best way to visualize data. Also in this case a bart chart will result in a 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:

enter image description here

like image 200
Jaap Avatar answered Nov 16 '22 16:11

Jaap


From plotrix documentation, there is a margin parameter mar - Margins around the pie. See default is set to mar=c(4,4,4,4) which is creating this white space.

pie3D(x,edges=NA,radius=1,height=0.1,theta=pi/6,start=0,border=par("fg"),
  col=NULL,labels=NULL,labelpos=NULL,labelcol=par("fg"),labelcex=1.5,
  labelrad=1.25,sector.order=NULL,explode=0,shade=0.8,mar=c(4,4,4,4),pty="s",...)

Try setting mar to smaller amounts, so add in your pie3D calls this option, e.g. mar =c(1,1,1,1) to lower all the margins.

like image 24
micstr Avatar answered Nov 16 '22 16:11

micstr