2

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
...
...
Community
  • 1
  • 1
Sultan
  • 189
  • 2
  • 9
  • 1
    Have you tried something like `par(mar=c(2,2,2,0)+0.1)`? Alternatively, `lattice` and `ggplot2` both have great faceting functionality. – r2evans Apr 25 '16 at 22:55
  • I would love to use lattice, but I don't have that strong background. The ggplot2 did not show results on my machine !! – Sultan Apr 25 '16 at 22:58

1 Answers1

1

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.

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • I got error package `Error in library(tidyr) : there is no package called ‘tidyr’` . I tried to install the package and I got this tidyr' is not available (for R version 3.0.2)` – Sultan Apr 25 '16 at 23:53
  • 1
    The current version of `tidyr` (0.4.1) requires R version 3.1.0 or higher. You can update your version of R, or try installing an [older version of `tidyr`](https://cran.r-project.org/src/contrib/Archive/tidyr/) from source. Alternatively, you can use: `d2 <- cbind(d[, 1:2], Variable=rep(colnames(d)[-(1:2)], each=nrow(d)), Value=unlist(d[, -(1:2)]))` – jbaums Apr 25 '16 at 23:57
  • I updated R and installed the packages successfully, and I copied/pasted your code example for testing, it runs with out errors but the console did show the plot? Is there any missing code in your example? FYI: I used source("tests.R"), where tests.R contains your code example. – Sultan Apr 26 '16 at 00:26
  • 1
    If you're using `soruce`, you need to wrap the `ggplot` and `barchart` calls in `print()`, e.g.: `print(ggplot(d2, aes(x=Topic, y=Value, fill=Variable)) + geom_bar(stat='identity', position='dodge') + facet_wrap(~Project))` – jbaums Apr 26 '16 at 00:56
  • Last thing, is there any way to include the legends (bars colors information) within the plot table. Because I have 15 barplots (3X5) and the last cell will of the plot table will be empty and I would like to take advantage of that by adding the legends (bar colors) in that cell, rather than plot the legends in top of the graph. – Sultan Apr 26 '16 at 01:11
  • That issue is meaty enough to be posted as a separate question (but first try existing documentation (e.g., [this](https://stat.ethz.ch/R-manual/R-devel/library/lattice/html/interaction.html) and [this](http://latticeextra.r-forge.r-project.org/man/panel.key.html)) and SO solutions, e.g. [this](http://stackoverflow.com/questions/7270900/position-legend-in-first-plot-of-facet)) – jbaums Apr 26 '16 at 01:16