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

Show secondary axis lines without specifying secondary axes #3917

Closed
willgearty opened this issue Mar 30, 2020 · 3 comments
Closed

Show secondary axis lines without specifying secondary axes #3917

willgearty opened this issue Mar 30, 2020 · 3 comments

Comments

@willgearty
Copy link

I'm trying to make a plot with a border around the entire panel and also with tick marks at the very ends of the axes. Unfortunately, regardless of theme this means the tick marks don't line up with the panel border. This is especially apparent when you have a larger base_size:

ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_y_continuous(limits = c(10,50), expand = c(0,0)) +
    theme_bw(base_size=24)

image
(note the 10 and 50 y axis tick marks do not line up with the panel border)

Now, this makes sense, because the panel border is actually supposed to be within the axes, so let's just put some axes on:

ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_y_continuous(limits = c(10,50), expand = c(0,0)) +
    theme_bw(base_size=24) +
    theme(axis.line = element_line(lineend = "square"))

image

Well, that's not really what we want, because the panel border still doesn't line up with the axes. But how can I add right and top axes? The only way I know of is as follows:

ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_y_continuous(limits = c(10,50), expand = c(0,0), sec.axis = sec_axis(~.)) +
    theme_bw(base_size=24) +
    theme(axis.line = element_line(lineend = "square"), axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank())

image

But that feels pretty hacky to me. So my questions is: is there a way to add such an axis line without specifying sec.axis and then removing everything I don't want? Ideally the defaults for the the secondary axis theme elements would be element_blank() when there is no sec.axis specified, then the user could just turn them on as desired, e.g.:

ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_y_continuous(limits = c(10,50), expand = c(0,0)) +
    theme_bw(base_size=24) +
    theme(axis.line = element_line(lineend = "square"), axis.line.y.right = element_line(lineend = "square"))
@clauswilke
Copy link
Member

The problem is caused by clipping and goes away if you turn it off.

library(ggplot2)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_y_continuous(limits = c(10,50), expand = c(0,0)) +
  theme_bw(base_size=24) + 
  coord_cartesian(clip = "off")

Created on 2020-03-30 by the reprex package (v0.3.0)

@willgearty
Copy link
Author

willgearty commented Mar 31, 2020

@clauswilke Oh, wow, I had no idea clipping was causing the panel border to be half width, I just assumed it was designed like that, but that makes total sense now! That solution will definitely work for my current situation, but I can imagine cases where you might not want to turn clipping off (e.g. maybe you have elements outside of the plot area). I still think it'd be ideal to be able to style the secondary axes without going through the hassle of defining them in the x and y scales.

@thomasp85
Copy link
Member

it is unlikely that we will add this, and since your original issue was resolved I'm closing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants