Sultan
Sultan

Reputation: 187

Resize side-by-side multiple barplots in R

I used this answer to plot 15 barplots (side-by-side) in one PDF page. The issue is the figures quality becomes very small.

dat <- read.table("data.txt",header=TRUE)

pdf('plot.pdf')
m <- rbind(c(1,2,3), c(4,5,6),c(7,8,9),c(10,11,12),c(13,14,15))
layout(m)
barcols <- c("red","blue","green","orange","black","yellow")

sp <- split(dat, dat$Project)

sapply(seq_along(sp),
       function(x) {
         dd <- sp[[x]]
         m <- t(`rownames<-`(as.matrix(dd[, -(1:2)]), dd[, 1]))
         bp <- barplot(m,ylim=c(0, 0.4),beside=TRUE,col=barcols)
         title(main=names(sp[x]))
        # abline(h=0)
       }
)
plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend(0,0.6,c("C10","C10","C03","C11","C16","C08"),fill=barcols,cex=1.5)
dev.off()

The result of the above code as follow: enter image description here

Would it be possible to resize the figures, in order to make x-axis data readable?

The data format as follow:

Topic  Project  C10     C14     C03     C11     C16     C08
T1     P1       0.24    0.00    0.00    0.04    0.04    0.00
T2     P1       0.00    0.30    0.00    0.00    0.00    0.00
T3     P1       0.04    0.04    0.00    0.24    0.00    0.00
T4     P1       0.00    0.00    0.00    0.04    0.33    0.04
T5     P1       0.00    0.09    0.21    0.00    0.00    0.00
T6     P1       0.00    0.09    0.00    0.00    0.00    0.34
T1     P2       0.20    0.00    0.00    0.04    0.00    0.04
T2     P2       0.00    0.22    0.04    0.00    0.00    0.00
T3     P2       0.04    0.00    0.00    0.24    0.00    0.00
T4     P2       0.00    0.00    0.04    0.00    0.33    0.00
T5     P2       0.04    0.00    0.21    0.00    0.00    0.00
T6     P2       0.00    0.04    0.00    0.00    0.00    0.34
T1     P3       0.20    0.00    0.00    0.04    0.00    0.04
T2     P3       0.00    0.22    0.04    0.00    0.00    0.00
T3     P3       0.04    0.00    0.00    0.24    0.00    0.00
T4     P3       0.00    0.00    0.04    0.00    0.33    0.00
T5     P3       0.04    0.00    0.21    0.00    0.00    0.00
T6     P3       0.00    0.04    0.00    0.00    0.00    0.34
...
...

Upvotes: 2

Views: 532

Answers (1)

jbaums
jbaums

Reputation: 27388

Here's how you might do it with ggplot2 or lattice. These packages work with "long format" data. For your dataset, this means having the values of columns 3 through 8 stacked into a single column, with a second column indicating the name of the group (i.e. the original columns' names, e.g. C10, C14, etc.).

tidyr is convenient for reshaping to long format:

d <- read.table(text='Topic  Project  C10     C14     C03     C11     C16     C08
T1     P1       0.24    0.00    0.00    0.04    0.04    0.00
T2     P1       0.00    0.30    0.00    0.00    0.00    0.00
T3     P1       0.04    0.04    0.00    0.24    0.00    0.00
T4     P1       0.00    0.00    0.00    0.04    0.33    0.04
T5     P1       0.00    0.09    0.21    0.00    0.00    0.00
T6     P1       0.00    0.09    0.00    0.00    0.00    0.34
T1     P2       0.20    0.00    0.00    0.04    0.00    0.04
T2     P2       0.00    0.22    0.04    0.00    0.00    0.00
T3     P2       0.04    0.00    0.00    0.24    0.00    0.00
T4     P2       0.00    0.00    0.04    0.00    0.33    0.00
T5     P2       0.04    0.00    0.21    0.00    0.00    0.00
T6     P2       0.00    0.04    0.00    0.00    0.00    0.34
T1     P3       0.20    0.00    0.00    0.04    0.00    0.04
T2     P3       0.00    0.22    0.04    0.00    0.00    0.00
T3     P3       0.04    0.00    0.00    0.24    0.00    0.00
T4     P3       0.00    0.00    0.04    0.00    0.33    0.00
T5     P3       0.04    0.00    0.21    0.00    0.00    0.00
T6     P3       0.00    0.04    0.00    0.00    0.00    0.34', header=TRUE)

library(tidyr)
d2 <- gather(d, Variable, Value, -Topic, -Project)

head(d2)

##   Topic Project Variable Value
## 1    T1      P1      C10  0.24
## 2    T2      P1      C10  0.00
## 3    T3      P1      C10  0.04
## 4    T4      P1      C10  0.00
## 5    T5      P1      C10  0.00
## 6    T6      P1      C10  0.00    

Then, with ggplot2:

library(ggplot2)
ggplot(d2, aes(x=Topic, y=Value, fill=Variable)) +
  geom_bar(stat='identity', position='dodge') +
  facet_wrap(~Project)

enter image description here

Or with lattice:

library(lattice)
barchart(Value~Topic|Project, d2, groups=Variable, origin=0)

enter image description here

Both of the above are highly customisable. See the ggplot2 docs, and ?lattice::barchart.

Upvotes: 1

Related Questions