# Run the secondary axis helper
sec <- help_secondary(economics, primary = unemploy, secondary = psavert)
# Making primary plot
p <- ggplot(economics, aes(date)) +
geom_line(aes(y = unemploy), colour = "blue")
# For the secondary data, later we use the `proj` function from the helper
p <- p + geom_line(aes(y = sec$proj(psavert)), colour = "red")
# We feed the scale the secondary axis
p + scale_y_continuous(sec.axis = sec)
# Setup cross-correlated data
set.seed(42)
n <- 100
lag <- 20
dat <- cumsum(rnorm(n + lag))
df <- data.frame(
x = seq_len(n),
y1 = head(dat, n),
y2 = 10 + tail(dat, n) * 5 # offset and scale y2
)
# Choosing the cross-correlation function method.
sec <- help_secondary(df, y1, y2, method = "ccf")
ggplot(df, aes(x)) +
geom_line(aes(y = y1), colour = "blue") +
geom_line(aes(y = sec$proj(y2)), colour = "red") +
scale_y_continuous(sec.axis = sec)
Run the code above in your browser using DataLab