Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug when using horizontal stacking for plots with theme(strip.placement = "outside") #308

Closed
gbiele opened this issue Sep 19, 2022 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@gbiele
Copy link

gbiele commented Sep 19, 2022

If one tries to stack two plots patchwork and one of the plots uses facet_grid and theme(strip.placement = "outside"), this messes up the placement of tick marks.

Minimal reproducible example

The following works as expected:

df = 
  data.frame(
  inc = rep(1:10,3),
  prev = rnorm(30),
  grp = rep(c("a","b","x"),each=10)
)

p1 = ggplot(df,aes(x = inc, y = prev, color = grp)) + geom_point()

p2.a = ggplot(df,aes(x = inc, y = prev, color = grp)) + 
  geom_point() + 
  facet_grid(~grp) 

p1 / p2.a

Now modify plot p2.a and stack again:

p2.b = p2.a + theme(strip.placement = "outside")
p1 / p2.b

In the second plot, y-ticks marks and labels and y-label of the top plot are be moved to the right, so that the y-tick labels of the top plot are horizontally aligned with the y-label of the bottom plot. See also this figure.
MRE

Note: For this minimally reproducible example theme(strip.placement = "outside") does not change the plot. In my application I need theme(strip.placement = "outside") to manage the position of tick marks after using scale_y_continuous(sec.axis = dup_axis()), which I have omitted from the example.

@thomasp85 thomasp85 added the bug an unexpected problem or unintended behavior label Aug 7, 2023
@thomasp85
Copy link
Owner

This appears to have been fixed indirectly. Please reopen if you still experience this issue with the latest dev version

@ThomasPSpargo
Copy link

Hello, thanks for your work on this very helpful package!

I'm using patchwork v1.1.3 (with ggplot2 v3.4.0) and have found that this bug persists when faceting is used on both the plots. The bug occurs with either a vertical or horizontal plot layout and I can confirm that I can run the minimum reproducible example above without triggering the original bug.

I've updated the minimum reproducible example to show the new scenario for the bug. Thank you for looking into this.

## Generate demo data
df = data.frame(
  inc = rep(1:10,3),
  prev = rnorm(30),
  grp_1 = rep(c("a","b","c"),each=10),
  grp_2 = sample(c("x","y","z"),size=30,replace=TRUE)
)

## Generate the main plot elements
#p1.a: Create the left column, which only facets on grp_1, giving a 'total' column of plot panels
#p2.a: Create the right column, which facets on grp_1 AND grp_2
plot_base = ggplot(df,aes(x = inc, y = prev, color = grp_1)) + geom_point()+guides(color="none")
p1.a = plot_base+ facet_grid(rows=vars(grp_1),switch="y")
p2.a = plot_base+ facet_grid(cols=vars(grp_2),rows=vars(grp_1))


#Generate plot (x-axis text/ticks are as expected)
p1.a|p2.a

#Change strip placement
p1.b = p1.a+ theme(strip.placement = "outside")

#Re-generate plot with strip outside (x-axis text/ticks are as shifted for p2.a)
p1.b|p2.a

See also the figure output for the p1.b|p2.a patchwork:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants