I want to create a plot with 2 Y axis with different scales, using ggplot. The one on the left should be the temperature and the one on the right should be the daylength. I tried a bunch of different methods to do so, but nothing appears to work. Here's the most promising code I came up with:
maxtemp <- max(dataforplot$Mean.temp, na.rm = TRUE)
maxdaylength <- max(dataforplot$daylength)
mintemp <- min(dataforplot$Mean.temp, na.rm = TRUE)
mindaylength <- min(dataforplot$daylength)
scale = (maxtemp - mintemp) / (maxdaylength - mindaylength)
shift = mindaylength- mintemp
#Function to scale secondary axis
scale_function <- function(x, scale, shift){
return ((x)*scale - shift)
}
#Function to scale secondary variable values
inv_scale_function <- function(x, scale, shift){
return ((x + shift)/scale)
}
myplot <- ggplot(dataforplot, aes(x = julian, y = Mean.temp)) +
geom_line(aes(y = Mean.temp, color = "Temperature")) +
geom_line(aes(y = inv_scale_function(daylength, scale, shift), color = "Daylength")) +
scale_y_continuous(limits = c(mintemp, maxtemp),
sec.axis = sec_axis(~scale_function(., scale, shift), name="Daylength")) +
labs(x = "julian", y = "Temperature", color = "") +
scale_color_manual(values = c("orange2", "gray30"))
Here's a sample of my data:
dataforplot <- structure(list(julian = c(99, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158,
159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262,
263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288,
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299), Mean.temp = c(8.6,
7.4, 7.9, 5.45, 6.45, 6.7, 7.1, 6, 9.2, 8.7, 9.5, 9.9, 9.7, 9.8,
11.7, 12.75, 11, 9.1, 10, NA, 10.85, 11.35, 11.4, 12.35, 10.95,
10.5, 8.9, 10.35, 10.05, 9.7, 11.25, 11.15, 11, 8.95, 9.05, 10.15,
11.3, 12.85, 11.65, 12.05, 11, 11.6, 13.3, 16.05, 15.75, 13.6,
14.85, 13.95, 13.9, 12.55, 14.25, 12.35, NA, NA, 17.3, 15, 16.75,
15.6, 14.85, 13.8, 16.7, 13.4, 17, 17.3, 15.6, 15.35, 14.7, 14.35,
14.85, 15.2, 14.25, 14.65, 15.6, 15.6, 16.85, 14.9, 16.2, 19.75,
21, 22.15, 18.2, 16.55, 17.8, 19.05, 18.6, 15.25, 14.9, 17.85,
19.15, 19.35, 19.2, 19.05, 20.15, 20.7, 20, 19.4, 20.35, 18.3,
17.95, 16.65, 16.9, 18.2, 19.65, 20.7, 19.4, 19.05, 19.15, 20.85,
23.55, 24.6, 24.5, 24.6, 24.15, 23.5, 22.2, 21, 18.45, 18.75,
17.9, 20.05, 20.5, 20.95, 21.35, 20.4, 21, 20.4, 20.25, 20.2,
21.65, 21.7, 21.7, 23.25, 23.5, 22, 22.45, 21.65, 20.5, 21.85,
23, 19.2, 19, 19.6, 20.9, 21.65, 22.65, 21.05, 20.6, 19.75, 17.3,
NA, NA, 17.5, 15.1, 17.15, 18.75, 19.8, 19.35, 16.9, 17.75, 15.65,
16.15, 13.75, 15.1, 14.7, 15.8, 16.55, 16.65, 14.35, 16.3, 16.7,
17.6, 17.4, 17.4, 17.15, 15.5, 15.85, 16.7, 16.95, 15.95, 16.05,
15.9, 17.15, 15.7, 15.45, 15.3, 12.7, 12.6, 14.4, 14.3, 15, 15.15,
14.85, 13.7, 13.2, 14.05, 10.5, 10.2, 9.3, 9.3, 8.1, 7.7), daylength = c(13.3,
13.4, 13.4, 13.5, 13.6, 13.6, 13.7, 13.7, 13.8, 13.8, 13.9, 14,
14, 14.1, 14.1, 14.2, 14.2, 14.3, 14.4, 14.4, 14.5, 14.5, 14.6,
14.6, 14.7, 14.7, 14.8, 14.8, 14.9, 14.9, 15, 15, 15.1, 15.1,
15.2, 15.2, 15.3, 15.3, 15.4, 15.4, 15.5, 15.5, 15.5, 15.6, 15.6,
15.7, 15.7, 15.7, 15.8, 15.8, 15.8, 15.9, 15.9, 15.9, 16, 16,
16, 16, 16.1, 16.1, 16.1, 16.1, 16.2, 16.2, 16.2, 16.2, 16.2,
16.2, 16.2, 16.2, 16.2, 16.3, 16.3, 16.3, 16.3, 16.3, 16.3, 16.3,
16.2, 16.2, 16.2, 16.2, 16.2, 16.2, 16.2, 16.2, 16.2, 16.1, 16.1,
16.1, 16.1, 16, 16, 16, 16, 15.9, 15.9, 15.9, 15.8, 15.8, 15.8,
15.7, 15.7, 15.7, 15.6, 15.6, 15.6, 15.5, 15.5, 15.4, 15.4, 15.3,
15.3, 15.2, 15.2, 15.2, 15.1, 15.1, 15, 15, 14.9, 14.9, 14.8,
14.8, 14.7, 14.6, 14.6, 14.5, 14.5, 14.4, 14.4, 14.3, 14.3, 14.2,
14.2, 14.1, 14, 14, 13.9, 13.9, 13.8, 13.8, 13.7, 13.6, 13.6,
13.5, 13.5, 13.4, 13.3, 13.3, 13.2, 13.2, 13.1, 13.1, 13, 12.9,
12.9, 12.8, 12.8, 12.7, 12.6, 12.6, 12.5, 12.5, 12.4, 12.3, 12.3,
12.2, 12.1, 12.1, 12, 12, 11.9, 11.8, 11.8, 11.7, 11.7, 11.6,
11.5, 11.5, 11.4, 11.4, 11.3, 11.2, 11.2, 11.1, 11.1, 11, 10.9,
10.9, 10.8, 10.8, 10.7, 10.6, 10.6, 10.5, 10.5, 10.4, 10.4, 10.3,
10.2)), row.names = 100:300, class = "data.frame")
I would appreciate it so much if someone could help me with that!