-2

I am a new R user. So I have this proteomics data with more than 100 protein levels measured from 10 patients at 3 different time points. For each of these 100+ proteins I have used ggplot2 to make a boxplot facet (x=timepoint, y=expression level). The problem is, the protein names (titles of facets) are very long so they overlap with the ones next to them. Because there are so many of them I can't really use "\n" manually to seperate them into two rows. Is there a clever way to do this?

Also, I wanted to change the order of the three time points on the x axis. It was originally displayed in alphabetic order but i wanted it to have more biological significance.

Many thanks in advance.

Menglan
  • 175
  • 5
  • 3
    Please try to provide us with a minimal reproducible example. See this question for great examples: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Your second question is likely just a matter of using `reorder(X.Var,Other.Var)` in your `ggplot()` call. – Brandon Bertelsen Feb 13 '14 at 21:58
  • Thank you for the advice and I apologize for the inconvenience. – Menglan Feb 14 '14 at 17:22

1 Answers1

1

Is this what you had in mind?

set.seed(1)
# make up some data...
long.names <- c("Very Long Facet Label","Very, Very, Extremely Long Facet Label")
gg <- data.frame(x=rep(LETTERS[1:6],each=10),y=rnorm(120), z=rep(long.names,each=60))

library(ggplot2)
ggplot(gg) + geom_boxplot(aes(x,y))+facet_grid(.~z)

gg$x.ordered=factor(gg$x,levels=LETTERS[6:1])
gg$wrap <- sapply(strwrap(gg$z,20,simplify=F),paste,collapse="\n") # word wrap
ggplot(gg) + geom_boxplot(aes(x.ordered,y))+ facet_grid(.~wrap)

jlhoward
  • 58,004
  • 7
  • 97
  • 140