I have the following graph (below) that I would like to add a trend line to. However, I am having the same issue as this person has previously outlined: geom_smooth in ggplot2 not working/showing up.
However, I am unable to change the date to a data using as.date because it will not recognise only year e.g. 1999 as a date format (as opposed to dd-mm-yy).
Is there a simple way to get round this and add a trend line to the graph?
df <- data.frame(Year = c("2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022"),
Proportion = c(46, 51, 48, 66, 60, 73, 61, 73, 60, 68, 74, 65)
)
#Create plot, labels etc.
graph1 <- ggplot(df, aes(x = Year, y = Proportion)) +
geom_point() +
labs(title = "",
x = "Year",
y = "Proportion (%)") +
theme_classic(base_size = 20) +
geom_point(size = 3, colour = "red2") +
scale_y_continuous(limits = c(20, 80), breaks = seq(20, 80, by = 10))
graph1
as.Date(paste0(Year, "-01-01"))will come out as aDate-class columntransform(df, Date = as.Date(paste0(Year, "-01-01"))) |> ggplot(aes(x = Year, y = Proportion)) + geom_point() + ...x = as.numeric(Year)instead ofx = Year. Thegeom_smoothwill work perfectly well.group = 1to theaes()call will also work.