0

I am trying to create a plot to show trend over the years given some group variable (altitude), I want to create a line of best fit that shows the trend of all groups altogether. Similar to this question but I need it to be on the same plot, above the other lines

This is what I currently have Ggline plot with trends by group, I have not been able to do the best fit line in the same graph.

This is my code

library("ggplot2")
library("ggpubr")  
ggline(basal, "Years_", "Units", point.size = 2, size=1,
           shape = "Altitude", color = "Altitude")

This is my sample data

structure(list(Altitude = c("High altitude", "High altitude", 
"High altitude", "High altitude", "High altitude", "High altitude", 
"High altitude", "High altitude", "High altitude", "High altitude", 
"Medium Altitude", "Medium Altitude", "Medium Altitude", "Very High Altitude", 
"Very High Altitude", "Very High Altitude", "Very High Altitude", 
"100 MASL", "100 MASL", "100 MASL", "100 MASL", "100 MASL", "100 MASL", 
"UW", "UW", "UW", "UW", "UW", "NDL", "NDL", "Extreme altitudes", 
"Extreme altitudes", "Extreme altitudes", "Extreme altitudes", 
"Extreme altitudes", "Extreme altitudes", "Extreme altitudes", 
"Extreme altitudes", "Extreme altitudes", "Extreme altitudes", 
"Extreme altitudes"), Units = c(4153.54, 4330.29, 5565.94, 5892.6, 
5985.71, 6056.73, 6137.21, 6230.32, 6561.72, 6722.68, 6464.82, 
8172.53, 7875.8, 13015.03, 12366.81, 11990.35, 11797.41, 3121.51, 
3141.4, 3078, 2889.05, 2145.65, 2215.27, 13725.6, 15060.4, 14629, 
14425.35, 13824.9, 6461.84, 7103.28, 2632.66, 2724.67, 2740.61, 
2661.64, 2673.96, 2939.83, 3256.42, 3559.97, 3641.83, 3851.2, 
4412.65), Years_ = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 5, 10, 
0, 2, 4, 6, 0, 2, 4, 6, 8, 10, 0, 2, 4, 6, 8, 0, 5, 0, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10)), row.names = c(NA, 41L), class = "data.frame")

Thank you for any help,

Phil
  • 7,287
  • 3
  • 36
  • 66
DYtto
  • 11
  • 3

1 Answers1

1

I recommend you learn some basic ggplot2, which will make it much easier to add or change things in a plot. E.g.:

ggplot(dat, aes(Years_, Units, color = Altitude)) +
  geom_line() +
  geom_point() +
  geom_smooth(method = 'lm', col = 'black')

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94