0

When using help_secondary function in ggh4x package, is it possible to add points and change the names of two y-axes?

For instance, let’s consider examples provided in the manual of help_secondary function https://teunbrand.github.io/ggh4x/reference/help_secondary.html#examples

In the example with economics dataset, how to add points along with lines and change the names of both y- axes?

stefan
  • 90,330
  • 6
  • 25
  • 51
Rustam
  • 87
  • 6

1 Answers1

1

To add points along with the lines add geom_point layers. And to set the axis title you could use the name argument:

library(ggplot2)
library(ggh4x)

sec <- help_secondary(economics,
  primary = unemploy, secondary = psavert,
  name = "Secondary"
)

p <- ggplot(economics, aes(date)) +
  geom_line(aes(y = unemploy), colour = "blue") +
  geom_point(aes(y = unemploy), colour = "blue")

p <- p + geom_line(aes(y = sec$proj(psavert)), colour = "red") +
  geom_point(aes(y = sec$proj(psavert)), colour = "red")

p +
  scale_y_continuous(name = "Primary", sec.axis = sec)

stefan
  • 90,330
  • 6
  • 25
  • 51