Skip to content

Error when faceted plot has manually-placed legend #144

@patrickbarks

Description

@patrickbarks

I'm getting an error when I try to use patchwork on a faceted ggplot with a manually-placed legend (i.e. theme(legend.position = ...)). Here's a reprex.

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(mpg, disp, col = as.factor(gear))) +
  geom_point() +
  facet_wrap(~ cyl) +
  theme(legend.position = c(0.1, 0.7))

# plot by itself
p1

# plot within patchwork
p1 / p1
#> Error in FUN(X[[i]], ...): subscript out of bounds
wrap_plots(p1, p1)
#> Error in FUN(X[[i]], ...): subscript out of bounds

Created on 2020-01-06 by the reprex package (v0.3.0)

It works fine when I remove the facet_wrap(), or the theme(legend.position = ...) line. Something about both of them together seems to cause an issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions