-1

I have struggled to add secondary axes in R forever and really need help. My data frame includes changes in water elevation over a period of time for Well A and the water level for Well B which was pumped.

My data frame is as follows:

   Time_Date             Level_Change        Level
   2021-11-03 07:45:00   0.5                 18.5
   2021-11-03 08:00:00   0.6                 18.9
   2021-11-03 08:15:00   0.4                 19.5
   2021-11-03 08:30:00   0.7                 20.5
   2021-11-03 08:45:00   0.9                 21.4

What I am trying to do is plot Level_Change on the Primary (left) y-axis and Level on the secondary (right) y-axis. As you can see they have two very different ranges. I have tried it before but when I add in the sec.axis, my first axis always gets messed up. To clarify, I will add in the secondary axis and then my primary axis will also have a range of 0-30 which makes it difficult to view the data.

This is the Code I used below:

   Data %>%
   ggplot(aes(x = Time_Date, y = Level_Change, color = id)) +
   geom_point(size = 1) +
   geom_line(aes(x=Time_Date, y = MW25)) +
   scale_color_manual(values=c("red", "blue", "yellow", "green", "purple")) + 
   scale_x_datetime(breaks = scales::date_breaks("1 hour"), date_labels = "%H:%M") +
   scale_y_continuous(sec.axis = sec_axis(~.+15.12, name = "MW-25 GW Level")) +
   labs(title = "MW-25 Pumping Test Monitoring Well Response",
   x = "",
   y = "Change in Water Level (ft.)",
   legend = "Well ID") +
   theme(legend.title = element_blank()) + 
   theme_classic()

Really need any help I can get!

Thanks

JackWassik
  • 65
  • 6
  • 1
    What did you try? Where exactly did you get stuck? Did you try the methods at https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales. This seems like just a duplicate of that question in the absence of any differentiating details. – MrFlick Nov 10 '21 at 18:32
  • I have added the code I used. – JackWassik Nov 10 '21 at 18:37

1 Answers1

0

Thanks to the link provided, I was able to answer the question. The code here worked:

ggplot with 2 y axes on each side and different scales

JackWassik
  • 65
  • 6