Fitting trend-line on multiple plots using ggplot2

I have 4 time series plots on the same graph and I want to fit a trendline on all. I used ggplot and added the remaining two plots with the geom_line sub-function. When I used the geom_smooth function to fit the equation line, it only does it for the first graph leaving the other 2. Please, this is the link to the file I used for the code (https://drive.google.com/open?id=16k1cMMr1dthJOsppazVZ3cUCa1rRGF9m) and below is the code.

nf <- read.csv("F:/Project work/Publication/Under writing/Heat stress/nf_tas.csv", sep = ",", header = TRUE)
nf$date_mpi <- as.Date(as.character(nf$date_mpi))                                                                                 

nfu <- ggplot(data = nf, aes(x=nf$date_mpi, y=nf$mpi_nf))+  
  labs(title = "Annual temperature trends", x="Near-future projection", y="Temperature [°C]")+ 
  scale_y_continuous(limits = c(24,31)) +
  geom_line(aes(y=nf$mpi_nf, color='WRF-MPI-ESM MR'))+
  geom_line(aes(y=nf$hadgem_nf, color='WRF-HadGEM2-ES'))+
  geom_line(aes(y=nf$gfdl_nf, color='WRF-GFDL-ESM2M'))+ theme(legend.position = c(.1,.85))


nfu + theme(text = element_text(size = 12))
#> Error in eval(expr, envir, enclos): object 'nfu' not found

Created on 2020-03-22 by the reprex package (v0.2.1)

I'd appreciate if I could be assisted in this regard.

Hi @Mawuli, could you repost your code? I doesn't look like it came through.

1 Like

Hello Dromano

Thanks for flagging this across. The reprex has now be added.

Based on your introductory text, I was looking for a geom_smooth and I didnt see it in the code provided. Is there a second code showing your attempted use of it ? thanks

That's true. I have updated this now.

I am somehow unable to edit my earlier post so I've posted the code here please


nf <- read.csv("F:/Project work/Publication/Under writing/Heat stress/nf_tas.csv", sep = ",", header = TRUE)
nf$date_mpi <- as.Date(as.character(nf$date_mpi))                                                                                 

nfu <- ggplot(data = nf, aes(x=nf$date_mpi, y=nf$mpi_nf))+  
  labs(title = "Annual temperature trends", x="Near-future projection", y="Temperature [°C]")+ 
  scale_y_continuous(limits = c(24,31)) +
  geom_line(aes(y=nf$mpi_nf, color='WRF-MPI-ESM MR'))+ geom_smooth(method = "lm", se=FALSE, formula = my.formula)+
  geom_line(aes(y=nf$hadgem_nf, color='WRF-HadGEM2-ES'))+ geom_smooth(method = "lm", se=FALSE, formula = my.formula)+
  geom_line(aes(y=nf$gfdl_nf, color='WRF-GFDL-ESM2M'))+ theme(legend.position = c(.1,.85))
#> Error in ggplot(data = nf, aes(x = nf$date_mpi, y = nf$mpi_nf)): could not find function "ggplot"

Created on 2020-03-22 by the reprex package (v0.2.1)