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)
What I would love:
The best I can do in ggplot:
qplot(data=test, x=x, y=y)+facet_grid(facet.b~facet.a, labeller=label_both)
As indicated by @Hendy, similar to: add a secondary y axis to ggplot2 plots - make it perfect
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.
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.
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() .
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).
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)
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With