0

So I am using chemistry and precipitation data in the following two df:

    chem_df
    rain_df

I plotted the two datasets using ggplot() and in order to get 2 axes used the sex.axis function of the scale_y_continuous as follows:

    chem_rain_fig <- ggplot() +
    geom_point(data = chem_df, aes(x = Date, y = Temp)) +
    geom_line(data = rain_df, aes(x = Date, y = Rain)) + 
    scale_y_continuous(name = "Temp", sec.axis = (.~*, name = "Rain"))

But it keeps plotting both of the data sets to the original y-axis as follows: Graph with Issue I would like to just note that the rain data is between 0-10 cm, so that is why it follows the first axis and not the secondary axis with the limit(0,10)

Paul R
  • 208,748
  • 37
  • 389
  • 560
JackWassik
  • 65
  • 6

1 Answers1

0

This might answer your question

In essence, you have to manually transform your data and scale to make it appear the right size. Can't try without sample data but this should work, multiplying by 2 and dividing the scale:

    chem_rain_fig <- ggplot() +
    geom_point(data = chem_df, aes(x = Date, y = Temp)) +
    geom_line(data = rain_df, aes(x = Date, y = Rain*2)) + 
    scale_y_continuous(name = "Temp", sec.axis = sec_axis(~./2, name = "Rain"))
Andy Baxter
  • 5,833
  • 1
  • 8
  • 22