I cannot get geom_smooth() to work

I can't seem the geom_smooth function to work. I have tried the method = 'loess' and function = 'y ~ x' but I have several y-values and one x-value (which is a factor) which is the date.

This is the code that I have right now. Does any of you have som tips?

ggplot(allmortality, aes(Date)) +
geom_point(aes(y=C28), color = "firebrick") +
geom_point(aes(y=G1), color="red") +
geom_point(aes(y=G2), color = "darkgreen") +
geom_point(aes(y=G3), color = "blue") +
geom_point(aes(y=A), color = "orange") +
theme(axis.text.x = element_text(angle = 50, vjust = 1, hjust = 1)) +
theme(axis.title.x = element_text(vjust = 0, size = 12, face = "bold"),
axis.title.y = element_text(vjust = 2, size = 12, face = "bold")) +
theme(plot.caption = element_text(hjust = 0)) +
theme(panel.grid = element_blank())

Convert your data to a long format first:

Then you can change the code to something like:

ggplot(allmortality_long, aes(Date, value, colour = category)) +
geom_point() + 
geom_smooth()

I made my data long and used this code:

ggplot(longdata, aes(Date, Survival, color = Group)) +
geom_point() +
geom_point()

but I still get no lines through the points..

I used geom_smooth, a typo in my previous answer!

We'll need a reproducible example to help:
FAQ: How to do a minimal reproducible example ( reprex ) for beginners - meta / Guides & FAQs - Posit Forum (formerly RStudio Community)

Yes ofc! Below is a part of the dataset

longdata2 <- data.frame(Date = c("22-10-26", "22-11-07", "22-11-09", "22-11-10", "22-11-12", "22-11-13", "22-10-26", "22-11-07", "22-11-09", "22-11-10", "22-11-12", "22-11-13", "22-10-26", "22-11-07", "22-11-09", "22-11-10", "22-11-12", "22-11-13"),
Group = c("C28", "C28", "C28", "C28","C28", "C28", "G1", "G1", "G1", "G1", "G1", "G1", "G2", "G2", "G2", "G2", "G2", "G2"),
Survival = c("100", "94.77", "91.67", "87.17", "87.17", "87.17", "100", "99.56", "98.34", "97.96", "96.33", "94.29", "100", "98.33", "96.88", "95.54", "93.75", "92.41"))

library(ggplot2)

ggplot(longdata2, aes(Date, Survival, color = Group)) +
  geom_point(size = 3) +
  geom_smooth()

The problem is that you need to convert the dates and numbers from strings:

ggplot(longdata2, aes(as.Date(Date, format = "%y-%m-%d"), as.numeric(Survival), color = Group)) +
  geom_point(size = 3) +
  geom_smooth()

You should fix the dataframe first, but the above works.

Thank you so much!!!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.