Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to creat a trendline on a graph in R

i tried to use geom_smooth(method = "lm") but it doesnt work…

percentage.no.work <- cleanData %>% group_by(AREA) %>%
  summarise(percentage = mean(ESTIMATED.CITY.UNEMPLOYMENT))

ggplot() +
  geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
  geom_smooth(method = "lm") +
  theme_minimal() + ggtitle("Percentage Estimated City Unemployment") + 
  ylab("Percentage")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to provide the aesthetics for the geom_smooth as well. Either by including it in the ggplot() or in the geom_smooth() :

ggplot() +
  geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
  geom_smooth(aes(x=AREA, y=percentage), method = "lm") +
  theme_minimal() + ggtitle("Percentage Estimated City Unemployment") + 
  ylab("Percentage")

You can avoid repeating section of the code putting it in the ggplot()

ggplot(data=percentage.no.work, aes(x=AREA, y=percentage)) +
  geom_point(alpha=0.6, color="purple", size=2) +
  geom_smooth(method = "lm") +
  theme_minimal() + ggtitle("Percentage Estimated City Unemployment") + 
  ylab("Percentage")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading