Does anyone know how I can add an r^2 value to each of my lines on this graph with three different datasets? I used the ggplot2 and dplyr packages. Here's my code. I'm sort of new to so apologies if the answer is super obvious lol.
colours <- c("Crustiose" = "cadetblue", "Foliose" = "goldenrod1", "Fruticose" = "indianred")
data1<- lichendata %>% group_by(Elevation_Avg, Relative_Abundance_CR)
data2<- lichendata %>% group_by(Elevation_Avg, Relative_Abundance_FO)
data3<- lichendata %>% group_by(Elevation_Avg, Relative_Abundance_FR)
ggplot() +
geom_point(data = data1, aes(x = Elevation_Avg, y = Relative_Abundance_CR, colour = "Crustiose")) +
geom_smooth(data = data1, aes(x = Elevation_Avg, y = Relative_Abundance_CR), method ="lm", se = FALSE, colour = "cadetblue") +
geom_point(data = data2, aes(x = Elevation_Avg, y = Relative_Abundance_FO, colour = "Foliose")) +
geom_smooth(data = data2, aes(x = Elevation_Avg, y = Relative_Abundance_FO),method = "lm", se = FALSE, colour = "goldenrod1") +
geom_point(data = data3, aes(x = Elevation_Avg, y = Relative_Abundance_FR, colour = "Fruticose")) +
geom_smooth(data = data3, aes(x = Elevation_Avg, y = Relative_Abundance_FR), method = "lm", se = FALSE, colour = "indianred") +
labs( x = "Average Elevation (m)", y = "Relative Abundance", colour = "Growth Form") +
scale_colour_manual(values = colours)
I tried playing with the lm_eq section but I couldn't get it to show up on the graph for some reason. Maybe I'm calling the wrong dataset? Here's my graph without.
I think a combination of stat_poly_line and stat_poly_eq from the ggmisc https://cran.r-project.org/web/packages/ggpmisc/ggpmisc.pdf package should do what you are looking for. I also think you could make things a little easier by converting your data set to a long format rather than splitting into 3 seperate data sets:
data1<- lichendata %>% pivot_longer(cols= c("Relative_Abundance_CR", "Relative_Abundance_FO", "Relative_Abundance_FR"), names_to="type", values_to="Relative_Abundance")
data1 %>% ggplot(aes(Elevation_Avg,Relative_Abundance, colour=type))+geom_point()+stat_poly_line(se=F)+stat_poly_eq()
)
You will still need tweak the formatting of which colours etc... to your liking.