简体   繁体   中英

facets as in facet_grid, and axis.line as for facet_wrap?

I am trying to build a plot using facets as in , 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 , 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 . eg:

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))

情节柠檬代表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM