wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

plot_grid and theme_update(panel.grid.major) #63

Closed basille closed 7 years ago

basille commented 7 years ago

In general, I really like the minimalistic theme settings of cowplot. However, I also like having the major grid, and I find it easier to set it up globally at the beginning of my scripts. It works fine for single plots, but seems to confuse plot_grid, which plots a grid on top of the grid plot (and not only on top of single plots).

Here is a better explanation with simple iris plots:

library(cowplot)
i1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
    geom_point()
i2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
    geom_point()
i3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
    geom_point()
i4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
    geom_point()
plot_grid(i1, i2, i3, i4)

This works perfectly. I can use background_grid to add the grid manually to each plot:

i1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
    geom_point() + background_grid(major = 'xy')
i2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
    geom_point() + background_grid(major = 'xy')
i3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
    geom_point() + background_grid(major = 'xy')
i4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
    geom_point() + background_grid(major = 'xy')
plot_grid(i1, i2, i3, i4)

Still works as expected. Now if I update the theme to add the major grid to all plots:

theme_update(panel.grid.major = element_line(colour = "grey90",
    size = 0.2))
i1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
    geom_point()
i2 <- ggplot(iris, aes(Petal.Length, Petal.Width)) +
    geom_point()
i3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
    geom_point()
i4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) +
    geom_point()
plot_grid(i1, i2, i3, i4)

I get the additional grid on top of the combined plot. I didn't attach a picture of it, so please let me know if it's only happening on my computer (note: using Debian Testing, with R 3.3.2, ggplot 2.2.1 and cowplot 0.7.0).

Thanks for looking at this issue!

clauswilke commented 7 years ago

Hello,

I don't really have the time to look into this in detail right now, but I'm quite certain that the problem is the same as issue #60, which is a bug in ggplot2's theme_void(). Could you investigate, and maybe try a workaround similar to the one I posted in issue #60? And also, if it turns out to be a ggplot2 bug, file a report there?

Thanks!

basille commented 7 years ago

Thanks, I will try to investigate. At first glance, I'm not sure it's related, but I need to understand ggplot2 (and theming) mechanisms better before I can reach any conclusion. I'll keep you posted!

clauswilke commented 7 years ago

Here's how you test this: Load only ggplot2, set the theme with theme_update, make a plot, and then call the plot + theme_void(). If the grid doesn't go away then you know that theme_void() has a bug. It should undo your theme_update.

basille commented 7 years ago

Thank you for your guidance. I think I start to see it more clearly… I now have a minimal reproducible example using just ggplot2:

library(ggplot2)
qplot(1:10, (1:10)^2)                   # Basic plot
theme_update(panel.grid.major = element_line(colour = "red",
    size = 0.5))                        # I want major grid in red
qplot(1:10, (1:10)^2)                   # Works OK
qplot(1:10, (1:10)^2) + theme_void()    # Grid still here

But before I submit an issue, I'd like to be sure I understand correctly: Are we talking about a bug per se, or a limitation of ggplot2::theme_void which does not set panel.grid(.major/minor) to element_blank()?

Following your answer on #60, a workaround is:

assignInNamespace("theme_nothing", function(){
    theme_void() +
    theme(panel.grid.major = element_blank())
}, "cowplot")
clauswilke commented 7 years ago

I think it's a bug in ggplot2::theme_void, or alternatively in the ggplot2 theming mechanism in general. theme_void bills itself as a complete theme, which means that it should override all prior theme settings. However, that doesn't happen, as you can see from these examples.

basille commented 7 years ago

I just posted bug #2079 on ggplot2 issue tracking system. I'll keep you posted!

Thanks for helping me investigate, and finding a temporary workaround!

clauswilke commented 7 years ago

I've decided to reimplement theme_nothing() since this problem still isn't resolved for base ggplot2. Code is in the current development branch on github.

library(ggplot2)
qplot(1:10, (1:10)^2)                   # Basic plot
theme_update(panel.grid.major = 
             element_line(colour = "red", size = 0.5)) # I want major grid in red
qplot(1:10, (1:10)^2)                   # Works OK
qplot(1:10, (1:10)^2) + theme_void()    # Grid still here

library(cowplot)
qplot(1:10, (1:10)^2) + theme_nothing() # Grid now gone
basille commented 7 years ago

Thanks Claus for taking care of this!