I am trying to build a plot using facets as in facet-grid, so plots separate in the x and y axes using two factors, as with:
mtcars %>% ggplot() +
geom_point(aes(x=mpg, y=wt)) +
facet_grid(cyl ~ gear, scales="free") +
theme(panel.background = element_blank(), axis.line = element_line(size=0.5))
The problem with the above approach is that only axis lines are shown for the up and left-most plots, so it is difficult to differentiate plots without using a colored panel.background
.
This problem does not occur with facet-wrap, but this function will apparently not group factors in the two axes, or the strips will be always in one of the sides (according to strip.position
argument), but not up and right as for facet_grid
. e.g.:
mtcars %>% ggplot() +
geom_point(aes(x=mpg, y=wt)) +
facet_wrap(cyl ~ gear, scales="free") +
theme(panel.background = element_blank(), axis.line = element_line(size=0.5))
Question
Is it possible to create a plot with facet_grid
, but lines in all axes, or alternatively use facet_wrap
, but group factors as with facet_grid
?
I think you are looking for facet_rep_grid
in the lemon
package. Here is the code to produce the desired plot.
library(tidyverse)
library(lemon)
mtcars %>% ggplot() +
geom_point(aes(x=mpg,
y=wt)) +
facet_rep_grid(cyl ~ gear,
scales="free",
repeat.tick.labels = "all") +
theme(panel.background = element_blank(),
axis.line = element_line(size=0.5))