0

Evening all, hoping someone can help. I've spent the evening messing around with the ggplot package, but for some reason, I cannot get a regression line to show up using geom_smooth!

I've included the code here, unfortunately, the terminal doesn't actually flag it as an error however which is part of the reason why I'm stumped! I've run it line by line and each line executes with no flags. The graph itself even appears without a hitch, just missing the regression line!

code:

library(ggplot2) ScatterPlot <- ggplot(graph, aes (x= Specimen.date, y= Daily.lab.confirmed.cases)) + geom_point() + geom_smooth(method = lm) + theme(axis.text.x = element_text(angle=90)) ScatterPlot + ylim(0,5000) + labs(x = "Date") + labs(y = "Daily Cases") 

output:

> ScatterPlot <- ggplot(graph, aes (x= Specimen.date, y= Daily.lab.confirmed.cases)) + geom_point() + geom_smooth(method = lm) + theme(axis.text.x = element_text(angle=90)) > ScatterPlot + ylim(0,5000) + labs(x = "Date") + labs(y = "Daily Cases") 

geom_smooth() using formula 'y ~ x'

I've also tried converting columns to numeric but that didn't seem to work either ( x = as.numeric(), y = as.numeric().

any suggestions!?

Thank you

Michael

7
  • 1
    Try method = "lm" instead of method = lm Commented Apr 19, 2020 at 21:49
  • No joy i'm afraid, thanks though! Commented Apr 19, 2020 at 22:12
  • Tricky! If geom_smooth isn't working, you could try plotting the lm directly - see example by jim89 here: community.rstudio.com/t/insert-regression-model-into-ggplot2/… Good luck!! Commented Apr 19, 2020 at 23:37
  • 1
    please add sample data of graph. Commented Apr 20, 2020 at 2:52
  • did you try geom_smooth(method = lm, formula = y~x, se = FALSE) Commented Apr 20, 2020 at 10:35

1 Answer 1

1

Alright, we got there in the end. Mlcyo (See comments, Thanks!) got most of the way. Gave me a more helpful error at least. Heres how it was amended...:

graph[] <- lapply(graph, as.numeric) # Force entire dataframe into numeric type fit <- lm(Daily.lab.confirmed.cases ~ Specimen.date, data = graph) # Manually generate linear model geom_line(data = fortify(fit), aes(x = Specimen.date, y = .fitted)) # Insert after geom_point() 

Hope it helps someone else! Thanks again mlcyo, good luck with the PhD!

M

Sign up to request clarification or add additional context in comments.

2 Comments

Hi and glad you were able to get the plot to show... but it's not clear what was wrong in your initial case. You should be able to get the line to show with geom_smooth as well. Were you able to get the geom_smooth way of plotting the line to work after forcing the entire dataframe into numeric? Also, what was the output of str(graph) prior to doing your graph[] <- lapply(...) line in the answer? Was it one column in particular which was not numeric (but should have been)?
Glad you got it working Michael - for your original model did you check that the date object was converted correctly? I was running a lm with x=date last night and had some weird things happen - some tips here:stackoverflow.com/questions/28098324/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.