set limits for scondary axis in GGPLOT

Hi,
Im tring to create ggplot graph with secondary axis.

The command of the sec.axis is:
scale_y_continuous(sec.axis = sec_axis(~.)).

Right now the axis is between 0.4-1.6 and I wnat to change it to be 0.5-1.

How can I set the limits of the secondary axis?

Thank you!

Hi @john22,

If you aren't performing any sort of transformation, why would you want different limits on the secondary axis? What would this even look like?

I think you are better off achieving this level of customization on the secondary axis via a transformation and then using breaks and labels to control the aesthetics. Something like:

library(magrittr)
library(ggplot2)

mtcars %>% 
  ggplot(aes(hp, mpg)) +
  geom_point() + 
  scale_y_continuous(
    sec.axis = sec_axis(
      trans = ~ 235.21 / .x, 
      name = "Litres per 100km",
      breaks = seq(0, 30, by = 2)
    )
  ) +
  labs(x = "Horse Power (HP)", y = "Miles per gallon (MPG)") +
  theme_minimal(14)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.