ggplot2-assign new colors and lines to data

Hi all,
I created a plot showing the means and std.errors of 12 trees ("Geno_name" in the data set) sampled at each of 3 sites ("Site").
I would like to assign new colors to the trees, depending on which elevation they originate from ("Elv.grp "). In other words, I would like that all trees that grew at "Low" elevations are colored black, all trees that grew at "Mid" elevations are green and all trees at "High" elevations are blue.
One option to achieve that would be to use aes(color="Elv.grp") but if I do that I end up with a wrong plot structure ( means and SE were calculated over all "Elv.grp" and not over all "Geno_name" data)

In addition, I would like to connect each mean value of the same tree (e.g. all "L1" data points, all "L2" points etc.) with a line. I used "stat_summary(....geom="line")" but that did not help.

Your help would be highly appreciated.
Thank you,
Mike

My code and raw data can be found below:

#CODE:

##
###PLOT
##


#ordering:
dta$Geno_name<-ordered(dta$Geno_name,levels=c("L1","L2","L3","L4","M1","M2","M3","M4","H1","H2","H3","H4"))


# Calculate SE
data_summary <- function(x) {
  m <- mean(x)
  ymin <- m - sd(x) / sqrt(length(x))
  ymax <- m + sd(x) / sqrt(length(x))
  return(c(y = m, ymin = ymin, ymax = ymax))
}


#Plotting
plot_SLA <- ggplot(dta, aes(x =Site, y=SLA)) +
  
  #draw black mean and errorbar
  stat_summary(fun.data = data_summary, position = position_dodge(0.45), geom = "errorbar",aes(color=Geno_name), width = 0,size=1.5) +
  stat_summary(fun.data = data_summary, position = position_dodge(0.45), geom = "point",aes(color=Geno_name), size = 4.5) +
  #stat_summary(fun.data = data_summary, position = position_dodge(0.45), geom = "line",aes(color = Geno_name), alpha=0.3,size = 0.75) +
  # geom="line" not working

  
  #define lab and plot titles
  labs(title = "", x = "",   y="Specific leaf area (cm2/g)")+
  
  # Legend labels
  theme(legend.position="right")+
  scale_color_manual(name="Trees",values=c("red","red","red","red","orange","orange","orange","orange","blue","blue","blue","blue"))+

  #define theme 
  theme_classic() +
  
  theme(axis.text=element_text(face="bold",size=15),
        axis.title = element_text(color="black",face="bold",size=17),
        axis.line = element_line(colour = 'black', size = 1),
        axis.ticks = element_line(colour = "black", size = 1),
        legend.text=element_text(size=15),
        legend.title=element_text(size=17, face="bold"),
        plot.title = element_text(color="black", size=18, face="bold"))


plot_SLA

RAW DATA

> dput(dta)
structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("YU", 
"AF", "CRC"), class = c("ordered", "factor")), ID_field = c(3L, 
44L, 46L, 48L, 50L, 144L, 211L, 213L, 272L, 274L, 320L, 600L, 
601L, 6L, 8L, 51L, 53L, 55L, 195L, 199L, 215L, 217L, 277L, 279L, 
281L, 283L, 322L, 35L, 87L, 99L, 101L, 103L, 122L, 124L, 183L, 
185L, 539L, 258L, 11L, 13L, 36L, 38L, 40L, 126L, 128L, 130L, 
187L, 189L, 261L, 263L, 314L, 316L, 16L, 18L, 57L, 59L, 104L, 
146L, 148L, 150L, 229L, 231L, 290L, 581L, 349L, 351L, 353L, 395L, 
74L, 76L, 78L, 120L, 161L, 176L, 178L, 266L, 268L, 270L, 337L, 
339L, 371L, 42L, 81L, 85L, 491L, 494L, 180L, 221L, 223L, 300L, 
302L, 344L, 346L, 348L, 1L, 361L, 20L, 22L, 62L, 473L, 475L, 
134L, 505L, 167L, 242L, 244L, 303L, 323L, 325L, 24L, 26L, 65L, 
67L, 116L, 476L, 477L, 136L, 138L, 238L, 247L, 249L, 306L, 355L, 
357L, 29L, 31L, 33L, 70L, 72L, 132L, 480L, 142L, 308L, 326L, 
328L, 358L, 360L, 89L, 91L, 105L, 107L, 109L, 152L, 154L, 200L, 
204L, 251L, 292L, 296L, 585L, 586L, 94L, 96L, 110L, 112L, 114L, 
157L, 206L, 208L, 218L, 220L, 256L, 549L, 298L, 333L, 335L), 
    Block = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 1L, 1L, 
    3L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 1L, 
    1L, 3L, 2L, 3L, 4L, 4L, 4L, 1L, 1L, 3L, 3L, 4L, 1L, 1L, 1L, 
    2L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 2L, 1L, 1L, 3L, 
    3L, 4L, 2L, 2L, 2L, 4L, 4L, 2L, 3L, 4L, 4L, 4L, 1L, 3L, 3L, 
    3L, 4L, 2L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 1L, 2L, 3L, 3L, 1L, 
    1L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 1L, 
    1L, 2L, 2L, 3L, 1L, 1L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 4L, 1L, 
    1L, 2L, 2L, 4L, 1L, 1L, 2L, 4L, 4L, 2L, 2L, 2L, 3L, 3L, 1L, 
    1L, 2L, 2L, 3L, 3L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 2L, 2L, 3L, 
    3L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 2L, 3L, 3L, 4L, 
    4L, 1L, 1L, 2L, 3L, 3L), .Label = c("1", "2", "3", "4"), class = "factor"), 
    Plot = structure(c(5L, 26L, 26L, 26L, 26L, 18L, 39L, 39L, 
    14L, 14L, 29L, 40L, 40L, 5L, 5L, 26L, 26L, 26L, 34L, 34L, 
    39L, 39L, 14L, 14L, 14L, 14L, 29L, 17L, 33L, 38L, 38L, 38L, 
    4L, 4L, 33L, 33L, 46L, 4L, 7L, 7L, 17L, 17L, 17L, 4L, 4L, 
    4L, 33L, 33L, 4L, 4L, 25L, 25L, 9L, 9L, 28L, 28L, 39L, 22L, 
    22L, 22L, 44L, 44L, 20L, 27L, 39L, 39L, 39L, 14L, 31L, 31L, 
    31L, 47L, 26L, 31L, 31L, 5L, 5L, 5L, 34L, 34L, 6L, 21L, 32L, 
    32L, 12L, 12L, 32L, 42L, 42L, 23L, 23L, 37L, 37L, 37L, 2L, 
    2L, 15L, 15L, 29L, 10L, 10L, 16L, 16L, 29L, 1L, 1L, 24L, 
    30L, 30L, 15L, 15L, 29L, 29L, 43L, 10L, 10L, 16L, 16L, 45L, 
    1L, 1L, 24L, 43L, 43L, 15L, 15L, 15L, 29L, 29L, 10L, 10L, 
    16L, 24L, 30L, 30L, 43L, 43L, 35L, 35L, 41L, 41L, 41L, 24L, 
    24L, 35L, 35L, 3L, 21L, 21L, 33L, 33L, 35L, 35L, 41L, 41L, 
    41L, 24L, 35L, 35L, 41L, 41L, 3L, 3L, 21L, 33L, 33L), .Label = c("1", 
    "2", "3", "4", "5", "6", "8", "9", "10", "11", "12", "14", 
    "15", "16", "17", "18", "20", "21", "22", "23", "25", "26", 
    "29", "30", "31", "32", "33", "34", "35", "38", "39", "40", 
    "41", "42", "43", "46", "48", "49", "51", "52", "53", "55", 
    "57", "58", "59", "60", "64"), class = "factor"), Popul = structure(c(5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    7L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = c("CAFAUG", "CCRCOL", "JLAJAK", "KKHOPI", "KWFWIL", 
    "LBWBIL", "SCTMEX"), class = "factor"), Elv.m = c(1126L, 
    1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 
    1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 
    1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 1126L, 143L, 
    143L, 143L, 143L, 143L, 143L, 143L, 143L, 143L, 143L, 143L, 
    143L, 143L, 143L, 143L, 143L, 143L, 143L, 143L, 143L, 143L, 
    143L, 143L, 143L, 143L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
    26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 70L, 70L, 70L, 70L, 
    70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 1507L, 1507L, 
    1507L, 1507L, 1507L, 1507L, 1507L, 1507L, 1507L, 1507L, 1507L, 
    1507L, 1507L, 1507L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 
    1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 
    1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 
    1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 
    1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 
    1920L, 988L, 988L, 988L, 988L, 988L, 988L, 988L, 988L, 988L, 
    988L, 988L, 988L, 989L, 990L, 988L, 988L, 988L, 988L, 988L, 
    988L, 988L, 988L, 988L, 988L, 988L, 988L, 988L, 988L, 988L
    ), Elv.grp = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("High", "Low", 
    "Mid"), class = "factor"), Geno = structure(c(1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("121", "130", 
    "137", "142", "143", "149", "158", "164", "172", "180", "184", 
    "193"), class = "factor"), Geno_name = structure(c(5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 7L, 7L, 7L, 
    7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("L1", 
    "L2", "L3", "L4", "M1", "M2", "M3", "M4", "H1", "H2", "H3", 
    "H4"), class = c("ordered", "factor")), Trt. = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = "C", class = "factor"), Nr_leaves_collected..bag. = c(25L, 
    34L, 40L, 33L, 31L, 50L, 23L, 30L, 16L, 18L, 28L, 33L, 38L, 
    22L, 31L, 33L, 31L, 29L, 44L, 38L, 30L, 32L, 20L, 21L, 9L, 
    13L, 33L, 43L, 25L, 35L, 32L, 39L, 79L, 67L, 21L, 32L, 29L, 
    33L, 26L, 25L, 26L, 36L, 32L, 54L, 53L, 50L, 23L, 26L, 24L, 
    23L, 14L, 16L, 35L, 37L, 42L, 42L, 39L, 55L, 45L, 78L, 33L, 
    39L, 17L, 31L, 34L, 25L, 20L, 34L, 36L, 39L, 64L, 48L, 46L, 
    30L, 38L, 24L, 26L, 35L, 34L, 32L, 12L, 38L, 14L, 52L, 41L, 
    37L, 42L, 40L, 36L, 24L, 30L, 36L, 35L, 35L, 17L, 15L, 46L, 
    58L, 35L, 28L, 36L, 26L, 24L, 118L, 29L, 29L, 22L, 35L, 34L, 
    43L, 28L, 25L, 32L, 36L, 50L, 41L, 32L, 26L, 34L, 5L, 21L, 
    9L, 8L, 10L, 36L, 35L, 36L, 17L, 33L, 27L, 24L, 40L, 9L, 
    6L, 42L, 8L, 13L, 35L, 48L, 48L, 40L, 38L, 39L, 43L, 37L, 
    27L, 57L, 27L, 20L, 27L, 14L, 32L, 37L, 41L, 36L, 26L, 75L, 
    34L, 25L, 24L, 34L, 41L, 33L, 32L, 45L, 16L), Nr_leaves_measure = c(10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 9L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 5L, 10L, 
    9L, 8L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 9L, 
    6L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), Dry_wt_10_leaves_mg = c(451, 
    591.4, 592, 477.1, 346.2, 540.7, 683.4, 359.8, 561.3, 704.8, 
    637.8, 750.6, 294.1, 463.2, 431.2, 375.2, 553.9, 344.8, 480.6, 
    481.9, 809.6, 636.4, 583.2, 422.5, 1282.8, 1540.4, 355.1, 
    620.1, 751.4, 333.9, 550.6, 480.1, 475.2, 203.1, 241, 492.9, 
    541.5, 579.6, 332.7, 306.9, 435.1, 418, 350.6, 349.7, 241.4, 
    371.4, 203.5, 341.2, 459.8, 150.9, 141.2, 454.6, 519, 462.6, 
    383.1, 314.6, 306.6, 302.4, 343, 233.3, 451.4, 652.8, 1421.2, 
    449.7, 1187.9, 1408.8, 1445.8, 241.8, 259.8, 227, 294.1, 
    588, 344.6, 396.3, 304.5, 400.3, 495.3, 983.9, 795.4, 735.9, 
    718.7, 1862.8, 706, 419.8, 533.9, 2097.7, 748.1, 1016.9, 
    510.3, 508.7, 449.5, 473.8, 441.1, 521.5, 601.1, 359.1, 839.2, 
    1354.1, 1073.7, 807.3, 909.3, 1267.2, 2101.5, 551.4, 1264.6, 
    707.9, 606.9, 678, 668.5, 796.3, 887.3, 693.1, 954.4, 837, 
    1375.2, 1121.2, 375.7, 1820.9, 1974.8, 265.1, 863, 965.4, 
    466.4, 1026.9, 781.2, 1284, 593, 842.5, 693.3, 1194.3, 930.6, 
    1008.1, 937.8, 945.2, 457.7, 784.6, 1832.5, 784, 256.1, 404.4, 
    454, 425.8, 547.4, 612.6, 284.9, 247.8, 352.8, 639.9, 1075.9, 
    1268.9, 331.2, 345.2, 254.6, 187.4, 321.9, 170, 276.4, 597.5, 
    270, 222.6, 180.8, 437.7, 342.5, 117.3, 265.2, 342.1), Area_10_leaves_cm2 = c(40.42, 
    55.91, 50.9, 42.33, 31.7, 52.48, 101.37, 43.52, 81.23, 136.68, 
    97.58, 94.02, 35.85, 43.46, 37.09, 30.56, 47.42, 33.8, 50.24, 
    52.46, 91.07, 80.82, 54.65, 53.45, 136.8, 268.49, 34.27, 
    51.7, 64.98, 26.69, 49.05, 45.86, 55.42, 23.63, 35.08, 57.1, 
    57.96, 73.85, 29.73, 27.01, 37.34, 37.62, 31.54, 37.81, 32.45, 
    46.62, 26.16, 45.61, 62.53, 16.96, 15.21, 66.48, 39.53, 32.19, 
    29.93, 26.47, 23.7, 34.32, 37.43, 25.46, 45.94, 60.03, 137.3, 
    53.73, 106.33, 145.1, 128.23, 22.99, 24.3, 22.23, 28.63, 
    50.28, 41.35, 61.14, 42.79, 59.5, 67.9, 118.62, 101.19, 83.31, 
    63.48, 159.24, 59.93, 31.98, 55.21, 194.78, 83.33, 99.9, 
    43.49, 63.06, 75.58, 57.73, 47.19, 68.38, 51.66, 32.81, 66.9, 
    141.25, 90.14, 79.84, 88.02, 126.06, 190.97, 57.97, 153.03, 
    77.65, 78.88, 69.21, 76.01, 56.54, 58.94, 47.84, 70.01, 64.15, 
    126.06, 104.94, 36.15, 164.29, 143.72, 27.21, 126.88, 114.57, 
    69.42, 102.7, 64.32, 104.28, 51.24, 74.27, 59.81, 109.36, 
    84.11, 97.63, 125.11, 104.41, 41.61, 75.87, 174.93, 64.15, 
    23.79, 38.22, 43.53, 38.36, 94.5, 76, 34.04, 27.64, 47.62, 
    85.88, 117.8, 135.7, 27.52, 28.67, 21.78, 18.37, 31.7, 18.1, 
    38.34, 63.59, 35.36, 29.3, 20.73, 47.49, 64.2, 14.87, 36.98, 
    37.72), SLA = c(0.08962306, 0.094538383, 0.08597973, 0.088723538, 
    0.091565569, 0.097059367, 0.14833187, 0.120956087, 0.14471762, 
    0.193927355, 0.152994669, 0.125259792, 0.121897314, 0.093825561, 
    0.08601577, 0.081449893, 0.085611121, 0.098027842, 0.104535997, 
    0.108860759, 0.112487648, 0.1269956, 0.093707133, 0.126508876, 
    0.106641721, 0.174298883, 0.096508026, 0.083373649, 0.086478573, 
    0.079934112, 0.089084635, 0.095521766, 0.116624579, 0.116346627, 
    0.145560166, 0.115844999, 0.107036011, 0.127415459, 0.089359784, 
    0.088009123, 0.085819352, 0.09, 0.089960068, 0.108121247, 
    0.134424192, 0.12552504, 0.128550369, 0.133675264, 0.13599391, 
    0.112392313, 0.107719547, 0.146238451, 0.076165703, 0.069584955, 
    0.078125816, 0.084138589, 0.077299413, 0.113492063, 0.109125364, 
    0.109129876, 0.101772264, 0.091957721, 0.0966085, 0.119479653, 
    0.089510902, 0.102995457, 0.088691382, 0.095078577, 0.093533487, 
    0.097929515, 0.097347841, 0.085510204, 0.119994196, 0.154277063, 
    0.140525452, 0.148638521, 0.137088633, 0.120561033, 0.127219009, 
    0.113208316, 0.088326144, 0.085484217, 0.084886686, 0.076179133, 
    0.103408878, 0.092854078, 0.111388852, 0.098239748, 0.085224378, 
    0.123963043, 0.16814238, 0.12184466, 0.106982544, 0.131121764, 
    0.085942439, 0.091367307, 0.07971878, 0.104312828, 0.083952687, 
    0.09889756, 0.096799736, 0.099479167, 0.090873186, 0.10513239, 
    0.121010596, 0.109690634, 0.129971989, 0.102079646, 0.113702319, 
    0.071003391, 0.066426237, 0.069023229, 0.073354987, 0.076642772, 
    0.091666667, 0.093596147, 0.096220389, 0.090224614, 0.07277699, 
    0.102640513, 0.147022016, 0.118676196, 0.148842196, 0.100009738, 
    0.082334869, 0.081214953, 0.086408094, 0.088154303, 0.086268571, 
    0.091568283, 0.090382549, 0.096845551, 0.133407976, 0.110463394, 
    0.090911077, 0.096698955, 0.095459754, 0.08182398, 0.092893401, 
    0.094510386, 0.095881057, 0.090089244, 0.172634271, 0.124061378, 
    0.119480519, 0.111541566, 0.134977324, 0.13420847, 0.10948973, 
    0.106943022, 0.083091787, 0.083053302, 0.085545954, 0.098025614, 
    0.098477788, 0.106470588, 0.138712012, 0.106426778, 0.130962963, 
    0.131626235, 0.11465708, 0.108498972, 0.187445255, 0.126768968, 
    0.139441931, 0.110260158)), row.names = c(1L, 2L, 3L, 4L, 
5L, 8L, 11L, 12L, 13L, 14L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 26L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
39L, 40L, 41L, 42L, 43L, 44L, 46L, 47L, 52L, 53L, 54L, 55L, 56L, 
57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 67L, 68L, 69L, 70L, 
71L, 72L, 73L, 74L, 75L, 76L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 
85L, 86L, 88L, 92L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 
102L, 104L, 106L, 107L, 108L, 109L, 110L, 111L, 114L, 115L, 116L, 
117L, 118L, 119L, 120L, 121L, 122L, 123L, 125L, 126L, 127L, 128L, 
130L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 
143L, 144L, 145L, 146L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 
156L, 157L, 158L, 159L, 160L, 161L, 163L, 166L, 167L, 168L, 169L, 
170L, 171L, 172L, 173L, 174L, 175L, 177L, 178L, 179L, 181L, 182L, 
184L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 
196L, 197L, 198L, 199L, 200L, 201L, 203L, 204L), class = "data.frame")

Let me know if this is what you had in mind:

library(tidyverse)

dta$Geno_name <- ordered(dta$Geno_name, levels=paste0(rep(c("L","M","H"),each=4), 1:4))
dta$Elv.grp = factor(dta$Elv.grp, levels=c("Low","Mid","High"))

pd = position_dodge(0.45)

plot_SLA <- ggplot(dta, aes(x =Site, y=SLA, group=Geno_name, colour=Elv.grp)) +
  stat_summary(fun.y=mean, geom="line", position=pd, size=0.3, linetype="12") +
  stat_summary(fun.data=mean_se, position=pd, size=0.8, geom = "linerange") +
  stat_summary(fun.y=mean, position=pd, size=4, geom = "point") +
  stat_summary(fun.y=mean, geom="text", aes(label=substr(Geno_name,2,2)),
               position=pd, colour="white", size=3) +
  labs(title = "", x = "",   y="Specific leaf area (cm2/g)") +
  scale_color_manual(name="Trees", values=c("red","orange","blue")) +
  expand_limits(y=0) +
  scale_y_continuous(expand=expand_scale(mult=c(0,0.02))) +
  theme_classic()

plot_SLA

Rplot16

Maybe faceting would work better:

ggplot(dta, aes(x =Site, y=SLA, group=Geno_name, colour=Site)) +
  stat_summary(fun.data=mean_se, position=pd, size=0.8, geom = "linerange") +
  stat_summary(fun.y=mean, position=pd, size=4, geom = "point") +
  stat_summary(fun.y=mean, geom="text", aes(label=substr(Geno_name,2,2)),
               position=pd, colour="white", size=3) +
  labs(title = "", x = "",   y="Specific leaf area (cm2/g)") +
  scale_color_manual(name="Trees", values=c("red","orange","blue")) +
  expand_limits(y=0) +
  scale_y_continuous(expand=expand_scale(mult=c(0,0.02))) +
  theme_classic() +
  facet_grid(. ~ Elv.grp) +
  guides(colour=FALSE)

Or this:

ggplot(dta, aes(x =Elv.grp, y=SLA, group=substr(Geno_name,2,2), colour=substr(Geno_name,2,2))) +
  stat_summary(fun.y=mean, geom="line", position=pd, size=0.3, linetype="11") +
  stat_summary(fun.data=mean_se, position=pd, size=0.8, geom = "linerange") +
  stat_summary(fun.y=mean, position=pd, size=4, geom = "point") +
  stat_summary(fun.y=mean, geom="text", aes(label=substr(Geno_name,2,2)),
               position=pd, colour="white", size=3) +
  labs(title = "", x = "",   y="Specific leaf area (cm2/g)") +
  scale_color_manual(name="Trees", values=c("red","orange","blue","purple")) +
  expand_limits(y=0) +
  scale_y_continuous(expand=expand_scale(mult=c(0,0.02))) +
  theme_classic() +
  facet_grid(. ~ Site) +
  guides(colour=FALSE)

2 Likes

Dear Joel,
This is amazing! Thank you very much for all your work!

Thanks to you I got insights into a variety of new techniques that I did not know about.

If it is ok with you I would have a couple of follow up questions:

1: Am I correct that you never used the function that I originally used:
¨
data_summary <- function(x) {
m <- mean(x)
ymin <- m - sd(x) / sqrt(length(x))
ymax <- m + sd(x) / sqrt(length(x))
return(c(y = m, ymin = ymin, ymax = ymax))
}

This was all done by stat_summary(fun.y..../ fun.data...)?

2: I have not worked with "tidyverse" sofar (I just downloaded a cheat sheet). What exactly did you use tidyverse for?
adding additional asthetics aes(...group=Geno_name, color=Elv.grp) also worked without tidyverse (at least when I played with your code)

3: I don't fully get how you "named" the data points using the code:
label=substr(Geno_name,2,2)

I read the description of the "substr" function. Somehow you told R to always remove the first letter of
the name. Since my names do only consist of 2 letters you told R to only use the second letter? But why would then (Geno_name, 0,2) not do the same?

Thanks a ton!
Mike

Yes. There's nothing wrong with your function, and it's good to know how to create custom summary functions for use with ggplot. However, since in this case there are built-in functions for calculating means and standard errors (or both), such as mean_se, I just used the built-in functions.

The tidyverse is actually suite of packages, including ggplot2, but also several other packages for data wrangling and analysis. In this case, I only used functions from ggplot2, but I typically just load the entire tidyverse when I'm wrangling, analyzing, and visualizing data.

The second and third arguments of substr represent the integer positions of the starting and ending characters in the string. In this case, we only want the second character in each element of Geno_name, so we want our substring to both start and end at the second position in the string. If use substr(x, 0, 2) we're telling substr to return a substring that starts at the zeroth position and ends with the second position. This means in practice that we get back the first two positions of the original string. For example, try running these:

substr(c("abc", "L1", "M12", "A"), 0, 2)
substr(c("abc", "L1", "M12", "A"), 1, 2)
substr(c("abc", "L1", "M12", "A"), 2, 2)
substr(c("abc", "L1", "M12", "A"), 2, 3)

By the way, substr is a base R function, but one of the packages loaded by library(tidyverse) is stringr, which has the str_sub function. str_sub works the same way as substr so we could have done str_sub(Geno_name, 2, 2). I used substr out of habit, but one of the advantages of tidyverse packages and functions is that they present a unified and consistent interface, including naming conventions for functions and function arguments.

1 Like

Hi.
Thank you very much for takin the time and explaining it to me!
I learnt a lot of new usefull R-stuff!
I really appreciate it.

Mike

1 Like

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