I am trying to add a regression line into my ggplot. Below is the code I am using to make the plot. I am wanting to plot the regression line in the first plot (L5andDownTempAvgPlot) but keep running into the following error. I am also wondering if there is a way to a line of best fit of the two lines in the ggplot (the one from DailyAvgL5 and DailyDownGaugeAvg). I’m not sure if this is possible, but if so that’s ultimately what I would like to be able to do.
#create plot of daily avgs
library(ggplot)
library(dplyr)
L5andDownTempAvgPlot <- (ggplot(NULL, aes(Date, mean_temp)) +
geom_line(data = DailyAvgL5, color = "black") +
geom_line(data = DailyDownGaugeAvg, color = "red") +
geom_smooth(data = DailyDownGaugeAvg, method = 'lm', formula = DailyDownGaugeAvg$Date~DailyDownGaugeAvg$mean_temp)
)
DownGaugeHeightAvgPlot <- (ggplot(DailyDownGaugeAvg, aes(Date, DailyGaugeHeightAvg)) +
geom_line(data = DailyDownGaugeAvg, color = "blue") +
geom_smooth(method = 'lm')
)
DownGaugeHeightAvgPlot + L5andDownTempAvgPlot
`geom_smooth()` using formula = 'y ~ x'
Warning message:
Failed to fit group -1.
Caused by error in `Ops.Date()`:
! * not defined for "Date" objects
I think this has something to do with the fact that I am trying to fit a regression line to data that is not detailed in the first argument in ggplot (data,…), but honestly not sure. I am really new to r so I am asking a lot of questions but I appreciate the help. I am also unable to display the correlation between the downstream gauge mean_temp and the L5 mean_temp but that is beyond this I believe. Thanks.
Matt Schaaf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.