Search code examples
rggplot2errorbar

following missing aesthetics in ggerrorbar


Maybe it`s already too late for working in my code. I have to redo my figure in ggplot2, and I am unabable to do so. My errorbars are not showing and I cannot understand why.

dev.new()
    ggplot() +
      geom_point(data = conf_intervals, aes(y = mean_ALA, x = mean_LIN, shape=trophic, 
                                            color=feeding_type_2,
                                            size = 3, 
                                            alpha = 0.5)) + scale_shape_manual("Trophic level", values = c(15:19)) +
    geom_errorbar(data = conf_intervals,aes(mean_ALA, ymin = mean_ALA - se_ALA,
                                            ymax = mean_ALA + se_ALA,)) +
    geom_errorbarh(data = conf_intervals,aes(mean_LIN, ymin = mean_LIN - se_LIN,
                                             ymax = mean_LIN + se_LIN)) +
    geom_point(data = CSIA_inverts_basal, aes(y=ALA.d13C, x=LIN.d13C, color=feeding_type_2))
      labs (title="Biplot of compound stable isotopes- Centroids with 95 % CI", subtitle="LIN VS. ALA",
            y=expression({delta}^13*C[ALA]~'\211'~VPDB),
            x=expression({delta}^13*C[LIN]~'\211'~VPDB)) + 
      # guides(color = FALSE, shape = FALSE) +
      theme_classic() 

Error: geom_errorbarh requires the following missing aesthetics: y Run rlang::last_error() to see where the error occurred. In addition: Warning message: Ignoring unknown aesthetics: x

enter image description here

Here is my example data:

dput(head(CSIA_inverts_basal))
structure(list(d13C.VPDB = c(-35.10487341, -34.85465717, -34.67216423, 
-34.06032315, -33.68548439, -33.4811546), d15.NAIR = c(-6.321847159, 
-5.384989361, -2.638749276, -4.986045928, -5.946279778, -6.648526348
), ALA.d13C = c(-43.2375195, -44.77813854, -42.1921855, -41.58363894, 
-39.156857, -40.33135344), LIN.d13C = c(-40.864145, -42.32043061, 
-41.4247005, -36.08156681, -39.45744387, -37.76516617), combi = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), levels = c("epilithon", "fresh.leaves", 
"gammarus", "grazing.ephemeroptera", "predatory.plecoptera", 
"salmonid.eyes", "shreddering.plecoptera", "submerged.leaves"
), class = "factor"), feeding_type = c("epilithon", "epilithon", 
"epilithon", "epilithon", "epilithon", "epilithon"), sampling.time = c("summer", 
"fall", "summer", "fall", "fall", "fall"), year = c(2018L, 2016L, 
2018L, 2016L, 2016L, 2016L), split = structure(c(2L, 2L, 2L, 
2L, 2L, 2L), levels = c("consumer", "resource"), class = "factor"), 
    split_2 = c("epilithon", "epilithon", "epilithon", "epilithon", 
    "epilithon", "epilithon"), split_3 = c("epilithon", "epilithon", 
    "epilithon", "epilithon", "epilithon", "epilithon"), feeding_type_2 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), levels = c("Epilithon", "Fresh leaves", 
    "Grazer", "Salmonid (Eyes)", "Predator", "Submerged leaves", 
    "Shredder"), class = "factor"), trophic = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), levels = c("Base", "Non-predatory invertebrate", 
    "Predatory invertebrate", "Predator"), class = "factor")), row.names = 2:7, class = "data.frame")

dput(conf_intervals)
structure(list(trophic = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 
4L), levels = c("Base", "Non-predatory invertebrate", "Predatory invertebrate", 
"Predator"), class = "factor"), feeding_type_2 = structure(c(1L, 
2L, 6L, 3L, 7L, 5L, 4L), levels = c("Epilithon", "Fresh leaves", 
"Grazer", "Salmonid (Eyes)", "Predator", "Submerged leaves", 
"Shredder"), class = "factor"), mean_ALA = c(-42.1, -39.7, -38.7, 
-45.7, -40.3, -42.8, -42.7), mean_LIN = c(-39.2, -40, -37.2, 
-40.8, -35.9, -36.7, -37.9), se_ALA = c(1.1, 1.1, 1.1, 2.2, 1.2, 
1.9, 0.4), se_LIN = c(1.1, 1.1, 0.6, 1.8, 0.9, 1.3, 0.6), N_ALA = c(12L, 
14L, 10L, 9L, 14L, 7L, 17L), LIN_N = c(12L, 14L, 10L, 9L, 14L, 
7L, 17L)), class = "data.frame", row.names = c(NA, -7L))

Can someone help me?


Solution

  • geom_errorbarh doesn't have an aesthetic called x. It has a y, an xmin and an xmax. I suspect you are mixing up the x and y variables in your errorbar calls too, so check these carefully. Also, you should move alpha and size outside of aes so they don't appear in the legend.

    It's also a good idea to make sure your code is formatted in such a way that it is easier to read and debug. Limiting your line length and using inheritance of the data passed to your initial ggplot call helps to simplify things a bit too.

    ggplot(conf_intervals) +
      geom_point(aes(y = mean_ALA, x = mean_LIN, shape = trophic, 
                     color = feeding_type_2), size = 3, alpha = 0.5) + 
      scale_shape_manual("Trophic level", values = c(15:19)) +
      geom_errorbar(aes(mean_LIN, ymin = mean_ALA - se_ALA,
                        ymax = mean_ALA + se_ALA)) +
      geom_errorbarh(aes(y = mean_ALA, xmin = mean_LIN - se_LIN,
                         xmax = mean_LIN + se_LIN)) +
      geom_point(data = CSIA_inverts_basal, 
                 aes(y = ALA.d13C, x = LIN.d13C, color = feeding_type_2)) +
      labs(title = "Biplot of compound stable isotopes- Centroids with 95 % CI", 
           subtitle = "LIN VS. ALA",
           y = expression({delta}^13*C[ALA]~'\211'~VPDB),
           x = expression({delta}^13*C[LIN]~'\211'~VPDB)) + 
      theme_classic() 
    

    enter image description here