Closed bappa10085 closed 9 months ago
Taking help from this answer, I have solved the issue like
sec <- help_secondary(df, primary = c(Tmax, Tmin, RH),
secondary = Rainfall, method = "max")
ggplot(df, aes(x = Date)) +
geom_col(aes(y = sec$proj(Rainfall), fill = "Rain")) +
geom_line(aes(y = Tmax, color = "Tmax")) +
geom_line(aes(y = Tmin, color = "Tmin")) +
geom_line(aes(y = RH, color = "RH")) +
scale_color_manual(values = c(Tmin = "green", Tmax = "red",
RH = "black", Rain = "blue"), aesthetics = c("fill", "color")) +
scale_y_continuous(sec.axis = sec) +
labs(y = "Max and Min temperature, RH", color = NULL, fill = NULL) +
guides(color = guide_legend(
override.aes = list(fill = c("blue", rep("transparent", 3)))
))
In help_secondary
, the default methods of projecting the secondary axis data is range
. But in documentation, it has not been mentioned. It would be better to mention it.
This is because the secondary helper doesn't know about geom_col()
, so it won't know that the data range should include 0 even though every value is more than 0.
I'd propose that a fix is to just include 0 as primary data, e.g.:
sec <- help_secondary(df, primary = c(Tmax, Tmin, RH, 0),
secondary = Rainfall)
I am trying to use
help_secondary
withgeom_col
likeAs you can see from the plot, the secondary y axis starts from -500. The 0 rainfall value is shown as -500 which ideally should be left out in the barplot. Another thing, how can I have the legend?