rggplot2

Issues with ggplot and the secondary axis


I am trying to plot a bar plot and line graph on the same plot with two different y axes. I understand that the secondary y-axis is just visual with ggplot so I am not sure if there is a solution to my issue. I want to see the bar plots more clearly. Currently, you can barely see them even though the secondary y axis is at the appropriate scale (because they are plotting to the primary axis).

This is a subset of my Data. You can see the max value of both my water and clam sample in this data set.

date         clam     water
2020-08-26   62.17    3.84
2020-08-31  277.85    1.93
2020-09-08   90.58    4.59
2020-09-22   97.0     0.81
2020-10-12  86.15     0.12
2020-10-20  14.90     0.65
2020-11-09  24.16     0.00
2020-12-10  20.07     0.11
2021-01-05   8.72     0.00
2021-02-10  14.05     0.00
2021-03-30  10.09     0.00
2021-04-22  10.96     0.00
2021-05-18  12.30     0.00
2021-06-01   8.60     0.00
2021-06-16  15.17     0.00
2021-07-07 1045.67    0.28
2021-07-22  61.49     0.41
2021-08-11  25.64     0.00
2021-08-26  15.71     0.16

p <- ggplot(turn, aes(x=date)) + 
  geom_line(aes(y = clam, group=1), colour = "black", size = 1) + 
  geom_bar(aes(y=water), stat = "identity", fill="cyan",colour ="#006000")+
  scale_y_continuous(sec.axis = sec_axis(trans=~./200, name = "water")) + 
  theme_minimal() + 
  theme(axis.text.x=element_text(angle=60, hjust=1)) 

p

This is a plot of my full data set. You can see that even though water concentrations are 4 they do not appear that way on the secondary y axis


Solution

  • You should update ggplot2 because your code doesn't work with the current version.

    You need to transform the water values if you misuse (in the opinion of the ggplot2 author) secondary axes like this.

    library(data.table)
    turn <- fread("date         clam     water
    2020-08-26   62.17    3.84
    2020-08-31  277.85    1.93
    2020-09-08   90.58    4.59
    2020-09-22   97.0     0.81
    2020-10-12  86.15     0.12
    2020-10-20  14.90     0.65
    2020-11-09  24.16     0.00
    2020-12-10  20.07     0.11
    2021-01-05   8.72     0.00
    2021-02-10  14.05     0.00
    2021-03-30  10.09     0.00
    2021-04-22  10.96     0.00
    2021-05-18  12.30     0.00
    2021-06-01   8.60     0.00
    2021-06-16  15.17     0.00
    2021-07-07 1045.67    0.28
    2021-07-22  61.49     0.41
    2021-08-11  25.64     0.00
    2021-08-26  15.71     0.16")
    
    turn[, date := as.Date(date)]
    
    library(ggplot2) #version 3.4.4
    
    #fixed code to work with supplied data
    p <- ggplot(turn, aes(x=date)) + 
      geom_line(aes(y = clam, group=1), colour = "black", linewidth = 1) + 
      geom_bar(aes(y=water*250), stat = "identity", fill="cyan",colour ="#006000")+
      scale_y_continuous(sec.axis = sec_axis(trans=~./250, name = "water"),
                         breaks = c(0, 250, 500, 750, 1000, 1250)) + 
      theme_minimal() + theme(axis.text.x=element_text(angle=60, hjust=1)) 
    p
    

    resulting plot