I have to plot a hydrograph where two y axis should be shown (rainfall and streamflow).
this is my code
ggplot(data = df, aes (x = `Sampling time`))+
geom_line(aes (y = y1), color = "blue")+
geom_bar(aes (y= y2), color = "black", stat = "identity")+
theme_minimal() +
scale_y_continuous(name = "Discharge (m3/s)"
, sec.axis = sec_axis(trans = ~rev(./70)
,name = "Precipitation (mm)"))
This is the results of the plot
So my problem is that the values of the secondary y axis (y2 or precipitation) are "stucked or linked" to y1 (the values of discharge). Geom_bar values (precipitation)should be plotted on top of the graph because that is where the 0 is. I used rev function to reverse the precipitation axis but the values (the bars) did not follow the same path. Does anyone know what is wrong in my code?
Any help will be truly appreciated