You can use ggplot2::sec_axis() but the secondary axis has to be based on a one-to-one transformation of the primary axis.
Data <- data.frame(stringsAsFactors = FALSE,
max_temp = c(33.1, 33, 31.5, 30.6, 28.9, 28.8, 27.9, 29.2, 30.9, 30.6,
29.8, 31.5, 33.4, 34.4, 29.6, 28.5, 28.7, 29.3, 28.1, 27.9,
29.6, 30.5, 30.1, 31.7),
min_temp = c(19.4, 19.8, 19.2, 19.1, 18.7, 18.4, 17.6, 17.7, 17.9, 18.2,
17.9, 18.6, 18.3, 17.7, 17.8, 19.4, 18, 17.6, 17.8, 17.8,
17.6, 18, 17.9, 18.2),
rainfall = c(45L, 67L, 209L, 180L, 131L, 115L, 104L, 184L, 133L, 121L, 80L,
36L, 43L, 38L, 101L, 193L, 208L, 79L, 81L, 94L, 109L, 189L,
156L, 35L),
date = c("Jan-80", "Feb-80", "Mar-80", "Apr-80", "May-80",
"Jun-80", "Jul-80", "Aug-80", "Sep-80", "Oct-80",
"Nov-80", "Dec-80", "Jan-81", "Feb-81", "Mar-81",
"Apr-81", "May-81", "Jun-81", "Jul-81", "Aug-81", "Sep-81",
"Oct-81", "Nov-81", "Dec-81"))
library(tidyverse)
library(lubridate)
Data %>%
mutate(date = dmy(paste("01-", date)), rainfall = rainfall / 5) %>%
gather(variable, temp, -date) %>%
ggplot(aes(x = date, y = temp, color = variable)) +
geom_line() +
scale_x_date(date_labels = "%b-%y",
date_breaks = "1 month",
minor_breaks = "1 month",
expand = c(0,0)) +
scale_y_continuous(sec.axis = sec_axis(~ . * 5, name = "Rainfall")) +
theme_minimal() +
theme(axis.text.x = element_text(angle=45, hjust=1, vjust = 1))
