Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R/ggplot: Vertical strip text with facet_wrap

I'm using ggplot in R to plot several conditions with facet_wrap. I'd like to put the strip with the plot name on the vertical axis on the right instead of on top.

This is an example:

library(ggplot2)
dat<- data.frame(name= rep(LETTERS[1:5], each= 4), value= rnorm(20), time= rep(1:5, 4))
gg<- ggplot(data= dat, aes(x= time, y= value)) +
    geom_point() +
    facet_wrap(~ name, ncol= 1)

enter image description here Here the plot names (A, B, C, D, E) are on top, I'd like them to be on the right as in here:

gg + facet_grid(name ~ .)

enter image description here

Is there an easy switch to do it? (I'm not using facet_grid since I'd like to use the options nrow and ncol that come with facet_wrap).

Thanks! Dario

sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.3.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4       grid_3.0.1        
 [5] gtable_0.1.2       labeling_0.2       MASS_7.3-29        munsell_0.4.2     
 [9] plyr_1.8.1         proto_0.3-10       RColorBrewer_1.0-5 Rcpp_0.11.0       
[13] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2      tools_3.0.1       
like image 907
dariober Avatar asked Feb 12 '15 14:02

dariober


People also ask

What does Facet_wrap do in Ggplot?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner.

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

How do you remove facet wrap labels?

Remove labels from Facet plot To remove the label from facet plot, we need to use “strip. text. x” argument inside the theme() layer with argument 'element_blank()'.


2 Answers

For future reference, you can now use strip.position="right" to put them on the right side, e.g.:

library(ggplot2)
dat<- data.frame(name= rep(LETTERS[1:5], each= 4), value= rnorm(20), time= rep(1:5, 4))
gg<- ggplot(data= dat, aes(x= time, y= value)) +
    geom_point() +
    facet_wrap(~ name, ncol= 1, strip.position="right")
like image 117
Zeke Avatar answered Oct 16 '22 17:10

Zeke


If you are willing to have the facet labels on the left hand side of the facets then there is a simple solution where the y-axis becomes the x-axis and the x-axis becomes the y-axis.

library(ggplot2)
library(gridExtra)
library(gridGraphics)

# standard plot, facet labels on top
ggplot(diamonds) + 
  aes(x = carat, y = price) + 
  geom_point() + 
  facet_wrap( ~ cut)

enter image description here

# Use the gridExtra and gridGraphics utilities to rotate the plot.
# This requires some modifications to the axes as well.  Note the use of 
# a negative carat in the aes() call, and text modifications with theme()
grid.newpage()
pushViewport(viewport(angle = 90))
grid.draw(ggplotGrob(

  ggplot(diamonds) + 
    aes(x = price, y = -carat) + 
    geom_point() + 
    facet_wrap( ~ cut) + 
    scale_y_continuous(name = "Carat", breaks = -seq(0, 5, by = 1), labels = seq(0, 5, by = 1)) + 
    theme(axis.text.y = element_text(angle = 270), 
          axis.title.y = element_text(angle = 270), 
          axis.text.x = element_text(angle = 270))
    ))

enter image description here

Moving the facet_wrap labels to the right hand side of the rotated graphic would be done with a rotation angle of -90. However, that would have the effective x-axis on top of the plots. You'd need to work on code for moving the y-axis labels from the left to right side of the "standard" plot and then rotate as illustrated here by -90.

like image 31
Peter Avatar answered Oct 16 '22 16:10

Peter