'ggplot2 secondary axis with different amount of data points than other data plotted on chart in R

through my searching so far I have not been able to produce a graph with a secondary axis that has a different amount of data points.

I've included a graph of what I am trying to produce. The scatterplot was produced in R but the secondary axis (temperature in red) was produced in excel and pasted on top of the graph. While this works for basic representation I'd like it to line up consistently with the x-axis without doubt. This is why I'd like to do it all within the same script.

Desired graph with secondary y-axis

I've included code to produce the scatter plot and a screenshot of some data for the secondary axis showing temperature data. Unfortunately this is a pretty large data frame with over 7000 rows. I'm not sure if it can actually reproduce what is needed based on this sample code but I hope it can at least lead to some insight.

library("ggpubr")
ggscatter(threerich1, x = "Date", y = "Richness",
      color = "DSG.Station", palette = "jco",
      shape = "DSG.Station") + ggtitle("37M") +
theme(plot.title=element_text(hjust = 0.5), axis.text.x = element_text(
angle = 90, vjust = 0.5, hjust = 1), legend.position="none")+
scale_x_date(breaks = seq(as.Date('2016-01-01'), as.Date('2020-02-01'),
                        by='4 month'), date_labels= '%m-%Y')+ ylim(0,15)

structure(threerich1[1:7686, c(3,9,88)])
        Date DSG.Station Richness
1: 2016-05-24         37M        7
2: 2016-05-24         37M        7
3: 2016-05-24         37M        7
4: 2016-05-24         37M        7
5: 2016-05-24         37M        7
---                                
7682: 2020-01-07         37M        2
7683: 2020-01-07         37M        2
7684: 2020-01-07         37M        2
7685: 2020-01-07         37M        2
7686: 2020-01-07         37M        2

dput(threerich1[1:200, c(3,9,88)])
structure(list(Date = structure(c(16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 16945, 
16945, 16945, 16945, 16945, 16945, 16959, 16959, 16959, 16959, 
16959, 16959, 16959, 16959, 16959, 16959, 16959), class = "Date"), 
DSG.Station = c("37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", "37M", 
"37M", "37M", "37M", "37M", "37M"), Richness = c(7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7)), row.names = c(NA, -200L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x7f9d2f8064e0>)

Sample of temperature data



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source