2

I have a set of code that produces multiple plots using facet_wrap:

ggplot(summ,aes(x=depth,y=expr,colour=bank,group=bank)) +
geom_errorbar(aes(ymin=expr-se,ymax=expr+se),lwd=0.4,width=0.3,position=pd) +
geom_line(aes(group=bank,linetype=bank),position=pd) +
geom_point(aes(group=bank,pch=bank),position=pd,size=2.5) +
scale_colour_manual(values=c("coral","cyan3", "blue")) +
facet_wrap(~gene,scales="free_y") +  
theme_bw()

With the reference datasets, this code produces figures like this:

multiplot example

I am trying to accomplish two goals here:

  1. Keep the auto scaling of the y axis, but make sure only 1 decimal place is displayed across all the plots. I have tried creating a new column of the rounded expr values, but it causes the error bars to not line up properly.
  2. I would like to wrap the titles. I have tried changing the font size as in Change plot title sizes in a facet_wrap multiplot, but some of the gene names are too long and will end up being too small to read if I cram them on a single line. Is there a way to wrap the text, using code within the facet_wrap statement?
Community
  • 1
  • 1

3 Answers3

3

Probably cannot serve as definite answer, but here are some pointers regarding your questions:

  1. Formatting the y-axis scale labels.

First, let's try the direct solution using format function. Here we format all y-axis scale labels to have 1 decimal value, after rounding it with round.

formatter <- function(...){
  function(x) format(round(x, 1), ...)
}

mtcars2 <- mtcars
sp <- ggplot(mtcars2, aes(x = mpg, y = qsec)) + geom_point() + facet_wrap(~cyl, scales = "free_y")
sp <- sp + scale_y_continuous(labels = formatter(nsmall = 1))

enter image description here

The issue is, sometimes this approach is not practical. Take the leftmost plot from your figure, for example. Using the same formatting, all y-axis scale labels would be rounded up to -0.3, which is not preferable.

The other solution is to modify the breaks for each plot into a set of rounded values. But again, taking the leftmost plot of your figure as an example, it'll end up with just one label point, -0.3

Yet another solution is to format the labels into scientific form. For simplicity, you can modify the formatter function as follow:

formatter <- function(...){
  function(x) format(x, ..., scientific = T, digit = 2)
}

enter image description here

Now you can have a uniform format for all of plots' y-axis. My suggestion, though, is to set the label with 2 decimal places after rounding.


  1. Wrap facet titles

This can be done using labeller argument in facet_wrap.

# Modify cyl into factors
mtcars2$cyl <- c("Four Cylinder", "Six Cylinder", "Eight Cylinder")[match(mtcars2$cyl, c(4,6,8))]

# Redraw the graph
sp <- ggplot(mtcars2, aes(x = mpg, y = qsec)) + geom_point() +
  facet_wrap(~cyl, scales = "free_y", labeller = labeller(cyl = label_wrap_gen(width = 10)))
sp <- sp + scale_y_continuous(labels = formatter(nsmall = 2))

enter image description here

It must be noted that the wrap function detects space to separate labels into lines. So, in your case, you might need to modify your variables.

zyurnaidi
  • 2,143
  • 13
  • 14
  • 1
    Thanks for the comprehensive response! I decided to leave the y axis scaling as is, since some plots require more decimal places than others. As for the wrapping titles, I used `data$gene <- gsub('_',' ', data$gene)` to replace all the _ with spaces. Then, by adding `labeller = labeller(gene = label_wrap_gen(width = 30)` to my `facet_wrap` statement, I was able to successfully wrap gene name. Thanks again! – Michael Studivan Jun 08 '16 at 01:52
  • @MichaelStudivan Great! Glad it helps – zyurnaidi Jun 08 '16 at 03:41
1

This only solved the first part of the question. You can create a function to format your axis and use scale_y_continous to adjust it.

df <- data.frame(x=rnorm(11), y1=seq(2, 3, 0.1) + 10, y2=rnorm(11))

library(ggplot2)
library(reshape2)

df <- melt(df, 'x')

# Before
ggplot(df, aes(x=x, y=value)) + geom_point() +
  facet_wrap(~ variable, scale="free")

enter image description here

# label function
f <- function(x){
  format(round(x, 1), nsmall=1)
}

# After
ggplot(df, aes(x=x, y=value)) + geom_point() +
  facet_wrap(~ variable, scale="free") +
  scale_y_continuous(labels=f)

enter image description here

JasonWang
  • 2,414
  • 11
  • 12
0

scale_*_continuous(..., labels = function(x) sprintf("%0.0f", x)) worked in my case.