Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a general label to facets in ggplot2?

I often have numeric values for faceting. I wish to provide sufficient information to interpret these faceting values in a supplemental title, similar to the axis titles. The labeller options repeat much unnecessary text and are unusable for longer variable titles.

Any suggestions?

The default:

test<-data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20)) qplot(data=test, x=x, y=y, facets=facet.b~facet.a) 

enter image description here

What I would love:

enter image description here

The best I can do in ggplot:

qplot(data=test, x=x, y=y)+facet_grid(facet.b~facet.a, labeller=label_both) 

enter image description here

As indicated by @Hendy, similar to: add a secondary y axis to ggplot2 plots - make it perfect

like image 494
Etienne Low-Décarie Avatar asked Jul 05 '12 22:07

Etienne Low-Décarie


People also ask

How do I label a facet grid in R?

Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

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.

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap() .

What is facet wrap in ggplot2?

17.1 Facet wrap This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as. table and dir . ncol and nrow control how many columns and rows (you only need to set one).


2 Answers

As the latest ggplot2 uses gtable internally, it is quite easy to modify a figure:

library(ggplot2) test <- data.frame(x=1:20, y=21:40,                     facet.a=rep(c(1,2),10),                     facet.b=rep(c(1,2), each=20)) p <- qplot(data=test, x=x, y=y, facets=facet.b~facet.a)  # get gtable object z <- ggplotGrob(p)  library(grid) library(gtable) # add label for right strip z <- gtable_add_cols(z, unit(z$widths[[7]], 'cm'), 7) z <- gtable_add_grob(z,                       list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),                           textGrob("Variable 1", rot = -90, gp = gpar(col = gray(1)))),                      4, 8, 6, name = paste(runif(2)))  # add label for top strip z <- gtable_add_rows(z, unit(z$heights[[3]], 'cm'), 2) z <- gtable_add_grob(z,                       list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),                           textGrob("Variable 2", gp = gpar(col = gray(1)))),                      3, 4, 3, 6, name = paste(runif(2)))  # add margins z <- gtable_add_cols(z, unit(1/8, "line"), 7) z <- gtable_add_rows(z, unit(1/8, "line"), 3)  # draw it grid.newpage() grid.draw(z) 

enter image description here

Of course, you can write a function that automatically add the strip labels. A future version of ggplot2 may have this functionality; not sure though.

like image 56
kohske Avatar answered Oct 09 '22 04:10

kohske


The secondary axis is now an option: https://ggplot2.tidyverse.org/reference/sec_axis.html

# Basic faceted plot p <- ggplot(mtcars, aes(cyl, mpg)) +   geom_point() +   facet_grid(vs ~ am)          # Create a simple secondary axis for the facets (use the appropriate scale_x function) p +    scale_y_continuous(sec.axis = sec_axis(~ . , name = "SECOND Y AXIS", breaks = NULL, labels = NULL)) +   scale_x_continuous(sec.axis = sec_axis(~ . , name = "SECOND X AXIS", breaks = NULL, labels = NULL)) 

Plot output

like image 34
LizLaw Avatar answered Oct 09 '22 04:10

LizLaw