Why does LOESS line not work for R Scatterplot

113 Views Asked by At

I am trying to create side-by-side scatterplots using two data frames (lead and lead2) in R Markdown in the same code chunk, I can successfully add a LOESS line to the first plot but the second one does not work. Why is that? and How do I modify my code to get the second plot to display a LOESS line? thanks!

Below is the code:


3.3 Scatterplot - lead-time to IF vs year of IF onset 

```{r,figures-side, fig.show="hold", out.width="50%"}

plot(lead$Yr,lead$leadtime, main="Leadtime by Year of Onset (reg-filled)",
   xlab="Year of Onset", ylab="Leadtime (Years)", pch=19)
lines(lowess(lead$IF_Yr,lead$leadtime), col="red") # lowess line (x,y)


plot(lead2$Yr,lead2$leadtime,main="Leadtime by Year of Onset (exc missing)",
   xlab="Year of Onset", ylab="Leadtime (Years)", pch=19)
lines(lowess(lead2$IF_Yr,lead2$leadtime), col="red") # lowess line (x,y)
```

result scatterplot 1 result scatterplot 2

1

There are 1 best solutions below

2
Hong On

I have worked out that the reason for LOESS line not to work in this case is because there are NA values for the second data frame, which must be filtered out!