Closed dylanjm closed 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 .
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...
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.
And finally, element_* has an argument inherit.blank
:
Should this element inherit the existence of an
element_blank
among its parents? IfTRUE
the existence of a blank element among its parents will cause this element to be blank as well. IfFALSE
any blank parent element will be ignored when calculating final element state.
So yeah, that should probably do it.
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!
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.
I'm trying to create a visualization that utilized the
facet_rep_grid()
function and also thereposition_legend()
function. When I do this, it seems to remove the repeated axis lines. I've created a small reprex usingiris
to demonstrate what I mean.Plot with repeated facet grid axes![image](https://user-images.githubusercontent.com/15963026/41480016-40b70258-708a-11e8-9378-74c2d1796191.png)
__Result after using reposition_legend()__
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)