19

I would like to use faceting, but also to control the number of rows and columns. So facet_wrap is preferred to facet_grid. But while facet_grid works, facet_wrap does not and gives an error: "At least one layer must contain all variables used for facetting". Why does this occur? How can I use facet_wrap here?

x <- rnorm(8)
y <- x + rnorm(8, sd=0.7)
dd <- data.frame(id=rep(1:4, 2), x=x, y=y)
pp <- ggplot(dd, aes(x, y)) + geom_point()
pp  
pp + facet_grid(. ~ id)  # works
pp + facet_wrap(. ~ id)  # error
alexperrone
  • 667
  • 5
  • 12

1 Answers1

19

Drop the . in facet_wrap because the formula is one-sided:

pp + facet_wrap(~ id)
alexperrone
  • 667
  • 5
  • 12
  • can you please take a look at my question? https://stackoverflow.com/questions/65676788/combining-different-types-of-graphs-together-r thanks – stats_noob Jan 12 '21 at 02:04