0

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 enter image description here

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

Drop
  • 119
  • 6
  • 1
    You need to transform the values of the geom that should go on the secondary axis as well. See numerous answers here: https://stackoverflow.com/q/3099219/11374827 – teunbrand Feb 03 '22 at 10:36

0 Answers0