stefanedwards / lemon

🍋 Lemon --- Freshing up your ggplots
https://cran.r-project.org/package=lemon
GNU General Public License v3.0
180 stars 11 forks source link

`reposition_legend()` removes `facet_rep_grid()` effect? #9

Closed dylanjm closed 6 years ago

dylanjm commented 6 years ago

I'm trying to create a visualization that utilized the facet_rep_grid() function and also the reposition_legend() function. When I do this, it seems to remove the repeated axis lines. I've created a small reprex using iris to demonstrate what I mean.

d <- ggplot(iris, aes(x = Petal.Width, fill = Species)) + 
  geom_histogram() + 
  facet_rep_grid(Species ~ .) + 
  theme_minimal() + 
  theme(axis.line.x = element_line(linetype = "dashed"))

Plot with repeated facet grid axes image

reposition_legend(d, "top right", panel = "panel-1-1")

__Result after using reposition_legend()__

image

The legend has moved to the place I would like it, but the repeated axes are gone now.

I'm using the latest CRAN version of library(lemon)

stefanedwards commented 6 years ago

That's... odd. Definitely shouldn't do that.

Thanks for sharing; I'll take a look as soon as possible.

On Fri, 15 Jun 2018, 18:54 Dylan McDowell, notifications@github.com wrote:

I'm trying to create a visualization that utilized the facet_rep_grid() function and also the reposition_legend() function. When I do this, it seems to remove the repeated axis lines. I've created a small reprex using iris to demonstrate what I mean.

d <- ggplot(iris, aes(x = Petal.Width, fill = Species)) + geom_histogram() + facet_rep_grid(Species ~ .) + theme_minimal() + theme(axis.line.x = element_line(linetype = "dashed"))

Plot with repeated facet grid axes [image: image] https://user-images.githubusercontent.com/15963026/41480016-40b70258-708a-11e8-9378-74c2d1796191.png

reposition_legend(d, "top right", panel = "panel-1-1")

Result after using reposition_legend()

[image: image] https://user-images.githubusercontent.com/15963026/41480055-5ed15676-708a-11e8-8c49-8077326cc207.png

The legend has moved to the place I would like it, but the repeated axes are gone now.

I'm using the latest CRAN version of library(lemon)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stefanedwards/lemon/issues/9, or mute the thread https://github.com/notifications/unsubscribe-auth/ADbQmq9d_ZEhBt2GgSn6NvGQLwsn6ygiks5t8-bKgaJpZM4Up3Vj .

stefanedwards commented 6 years ago

Update

The root cause is that when turning legends off with d + theme(legend.position='hidden'), our garnish on the axis lines are removed for some reason. I will look into it some more.

Edit: reposition_legend uses the theme function to turn off legends when rendering the remainder of the plot...

stefanedwards commented 6 years ago

This is a most excellent issue, you have found, and it has nothing to do with lemon. It is actually ggplot2 that causes the problem when adding multiple themes, but I am not sure what the expected behaviour should be. I would say, at least not this!

The cause is that axis.line.x inherits from axis.line. When axis.line is element_blank() in theme_minimal() and theme_gray() (to name a few), rendering the following behaves as expected.

d <- ggplot(iris, aes(x = Petal.Width, fill = Species)) + 
  geom_histogram()
d + theme_minimal() + theme(axis.line.x=element_line(linetype='dashed'))

Now add a third theme for hiding the legend,

d + theme_minimal() + theme(axis.line.x=element_line(linetype='dashed')) + theme(legend.position='hidden')

and somehow axis.line overrides axis.line.x.

A workaround would be to stop axis.line overriding with an element_blank(), and blanking axis.line.y if required:

d + theme_minimal() + theme(axis.line = element_line(), axis.line.y=element_blank(), axis.line.x=element_line(linetype='dashed'))

An annoying solution for now. And it appears the same applies to axis.ticks.

I will try to use a different approach for hiding the legend to avoid this problem appearing in my package.

stefanedwards commented 6 years ago

And finally, element_* has an argument inherit.blank:

Should this element inherit the existence of an element_blank among its parents? If TRUE the existence of a blank element among its parents will cause this element to be blank as well. If FALSE any blank parent element will be ignored when calculating final element state.

So yeah, that should probably do it.

https://ggplot2.tidyverse.org/reference/element.html

dylanjm commented 6 years ago

Wow, I didn't think this seemingly small issue would reach so far back into ggplot. Thank you very much for figuring this out. The workaround appears to work well. Thanks again!

stefanedwards commented 6 years ago

You are most welcome! Sometimes we stumble over the smallest things. :) And I have looked at the help page for element_blank on countless occasions, but never realised the importance of that one argument.