I’m struggling a bit to get the secondary axis the way I want.
Here is the data:
structure(list(date = structure(c(19723, 19724, 19725, 19726, 19727, 19728, 19729, 19730, 19731, 19732, 19733, 19734, 19735, 19736, 19737, 19738, 19739, 19740, 19741, 19742, 19743, 19744, 19745, 19746, 19747, 19748, 19749, 19750, 19751, 19752, 19753, 19754, 19755, 19756, 19757, 19758, 19759, 19760, 19761, 19762, 19763, 19764, 19765), class = "Date"), tmin = c(3.8, 2.8, 8.1, 8.2, 4.6, 3.6, 1.8, -0.8, 4.1, 4.7, 2.3, -0.4, 2.2, 5.8, 8.4, 11.1, 10, 10, 3.6, 2.7, 4, 3.1, 4.4, 5.9, 5.9, 5.9, 9.6, 8, 7.7, 5.9, 4.7, 4.6, 5.2, 5.9, 4.6, 4.6, 5.9, NA, 8.5, 7.9, 5.3, 4.3, 6), tmax = c(9.4, 8.2, 12.1, 10.6, 8.6, 9.6, 9.3, 6.2, 7.2, 8, 7.6, 6.9, 9, 8.6, 13.3, 13.7, 12.3, 14.3, 10.8, 9.9, 11, 10.6, 14.9, 17.5, 17.3, 18.8, 18, 16.3, 14.9, 13, 10.5, 16.8, 14.8, 15.2, 16.6, 14.9, 13.7, NA, 12.6, 11.7, 11.3, 8.3, 15.7), tmean = c(6.6, 5.5, 10.1, 9.4, 6.6, 6.6, 5.6, 2.7, 5.6, 6.4, 5, 3.2, 5.6, 7.2, 10.8, 12.4, 11.2, 12.2, 7.2, 6.3, 7.5, 6.8, 9.6, 11.7, 11.6, 12.4, 13.8, 12.2, 11.3, 9.4, 7.6, 10.7, 10, 10.6, 10.6, 9.8, 9.8, NA, 10.6, 9.8, 8.3, 6.3, 10.8), pcp = c(NA, 2, 2.7, 9.7, 0.1, NA, NA, NA, NA, 0.2, NA, NA, NA, 4.3, 2.6, 7.5, 1.1, 12.4, 16.3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.2, 6.5, 0.4, 0.1, NA)), row.names = c(NA, -43L), class = c("tbl_df", "tbl", "data.frame"))
And here is my code so far:
selected_year <- 2024 ggplot2::ggplot(data = plot_data, aes(x = date)) + ggplot2::geom_col(aes(y = pcp, fill = "pcp"), na.rm = TRUE) + ggplot2::geom_line(aes(y = tmean, color = "tmean")) + ggplot2::geom_line(aes(y = tmax, color = "tmax")) + ggplot2::geom_line(aes(y = tmin, color = "tmin")) + ggplot2::scale_fill_manual( values = c("pcp" = "#2c7bb6"), label = paste0("Daily pcp."), guide = guide_legend(order = 1)) + ggplot2::scale_color_manual( values = c("tmean" = "black", "tmin" = "blue", "tmax" = "red"), label = c("tmean" = "Daily mean temp.", "tmin" = "Daily min temp.", "tmax" = "Daily max temp.")) + ggplot2::scale_y_continuous( labels = function(x) paste0(x, "ºC"), breaks = round(seq(from = round(min(plot_data$ tmin, na.rm = TRUE) - 5), to = 45, by = 5) / 5) * 5, limits = c(min(plot_data$ tmin, na.rm = TRUE) - 5, 45), sec.axis = sec_axis(trans = ~., labels = function(x) paste0(x, "mm")) ) + ggplot2::scale_x_continuous( breaks = as.numeric(seq(ymd(paste0(selected_year, "-01-15")), ymd(paste0(selected_year, "-12-31")), by = "month")), labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), limits = c(as.numeric(ymd(paste0(selected_year, "-01-01"))), as.numeric(ymd(paste0(selected_year, "-12-31"))))) + ggthemes::theme_hc(base_size = 15) + ggplot2::labs( x = "", y = "", title = paste0("Precipitation and temperature in Madrid - Retiro ", selected_year), subtitle = paste0("Daily precipitation and daily mean, max and min temperatures")) + ggplot2::theme( plot.title = ggplot2::element_text(hjust = 1, face = "bold", family = "sans", size = 35), plot.subtitle = ggplot2::element_text(hjust = 1, size = 25), legend.background = ggplot2::element_blank(), legend.box.background = ggplot2::element_rect(fill = "white", color = "black", linewidth = 0.75), legend.position = c(0.1335, 0.8), legend.spacing = ggplot2::unit(0, "cm"), legend.margin = ggplot2::margin(r = 5, l = 5, b = 5), legend.title = element_blank())
The code draws the following plot:
What I want is to draw the bars for precipitation starting at the minimum temperature showed on the label for the primary axis (temperature). In this particular case, precipitation bars should start at -5ºC. However, this might change depending on data, as some locations may have different minimum temperatures across the year.
Minimum temperature label will always be (round(seq(from = round(min(plot_data$ tmin, na.rm = TRUE) - 5), to = 45, by = 5) / 5) * 5)[1]