Sophie Williams
Sophie Williams

Reputation: 348

Error: Aesthetics must be either length 1 or the same as the data (13): ymax

I am trying to plot a graph showing species optima and tolerances across three different models wap, lg and reg, however I keep getting the error Error: Aesthetics must be either length 1 or the same as the data (13): ymax I read it was something to do with the fill but I am sure that I have defined the fills correctly? I also tried just keeping species common to all three models so that there weren't any NAs in the dataframe but I got the same error. Can anyone work out the issue?



specoptima<-read.csv("regspecopt.csv",header=TRUE)

ggplot()+
geom_point(data = specoptima, aes(x = Species, y = wapopt), color = "red",pch=21,size=3)+
geom_point(data = specoptima, aes(x = Species, y = lgopt), color = "blue",pch=23,size=3)+
geom_point(data = specoptima, aes(x = Species, y = regopt), color = "green",pch=23,size=3)+
  xlab('Species') +
  ylab('Height m AHD')+theme_classic()+
  geom_errorbar(aes(x=specoptima$Species,ymin=specoptima$wapopt-specoptima$waptol, ymax=specoptima$waopt+specoptima$waptol), width=0)+
  geom_errorbar(aes(x=specoptima$Species,ymin=specoptima$lgopt-specoptima$lgtol, ymax=specoptima$lgopt+spectopima$lgtol), width=0)+
  geom_errorbar(aes(x=specoptima$Species,ymin=specoptima$regopt-specoptima$regtol, ymax=specoptima$regopt+specoptima$regtol), width=0)

structure(list(Species = structure(c(13L, 12L, 4L, 9L, 11L, 5L
), .Label = c("A.agglutinans", "A.exiguus", "A.subcatenulatus", 
"H.wilberti", "J.macrescens", "M.fusca", "P.hyperhalina", "P.ipohalina", 
"S.lobata", "T.inflata", "T.irregularis", "T.salsa", "Textularia"
), class = "factor"), wapopt = c(NA, 178.2315, 177.5775, 177.1053, 
169.4055, 167.8907), waptol = c(NA, 15.21344, 6.385151, 8.477989, 
10.844778, 9.444243), lgopt = c(190.3974, 187.1097, 177.6777, 
170.332, 173.4925, 174.8782), lgtol = c(8.236862, 4.204461, 12.198399, 
9.714885, 10.590835, 8.939749), regopt = c(190.3974, 186.8404, 
177.6699, 174.0947, 173.2112, 172.8087), regtol = c(8.609964, 
4.767529, 11.754856, 9.363322, 10.508812, 9.539666)), row.names = c(NA, 
6L), class = "data.frame")
  
  

Upvotes: 0

Views: 214

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 173813

Like Duck says, you had a couple of typos. But here's why you had a couple of typos - unnecessary repetition in your code. The more times you have to type the name of you data frame, the more likely you are to have a typo. Long and unwieldy lines of code with minimal spacing between operators also makes in harder to see these.

Your geoms can all inherit their data from the ggplot call, and you don't need to do specoptima$variable inside your geom or stat calls, so your code simplifies to:

ggplot(data = specoptima, aes(x = Species)) +
  geom_point(aes(y = wapopt), color = "red", pch = 21, size = 3) +
  geom_point(aes(y = lgopt), color = "blue", pch = 23, size = 3) +
  geom_point(aes(y = regopt), color = "green", pch = 23, size = 3) +
  geom_errorbar(aes(ymin = wapopt - waptol, ymax = wapopt + waptol), width = 0) +
  geom_errorbar(aes(ymin = lgopt - lgtol, ymax = lgopt + lgtol), width = 0) +
  geom_errorbar(aes(ymin = regopt - regtol, ymax = regopt + regtol), width = 0) +
  xlab('Species') +
  ylab('Height m AHD') +
  theme_classic()

enter image description here

Or even better, reshape your data to avoid repeated geom calls:

tidyr::pivot_longer(specoptima, cols = -1,
                    names_sep = -3, names_to = c("type", "b")) %>% 
  tidyr::pivot_wider(names_from = b) %>%
  mutate(ymin = opt - tol, ymax = opt + tol) %>%
  ggplot(aes(x = Species, color = type)) +
  geom_point(aes(y = ymin), pch = 23) + 
  geom_point(aes(y = ymax), pch = 23) +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0, color = "black")

Upvotes: 5

Duck
Duck

Reputation: 39595

Try this. The issue was that you were calling a variable that was not present in your data (literally you misspelled its name). Here the code:

library(ggplot2)
#Code
ggplot()+
  geom_point(data = specoptima, aes(x = Species, y = wapopt), color = "red",pch=21,size=3)+
  geom_point(data = specoptima, aes(x = Species, y = lgopt), color = "blue",pch=23,size=3)+
  geom_point(data = specoptima, aes(x = Species, y = regopt), color = "green",pch=23,size=3)+
  xlab('Species') +
  ylab('Height m AHD')+theme_classic()+
  geom_errorbar(aes(x=specoptima$Species,ymin=specoptima$wapopt-specoptima$waptol,
                    ymax=specoptima$wapopt+specoptima$waptol), width=0)+
  geom_errorbar(aes(x=specoptima$Species,ymin=specoptima$lgopt-specoptima$lgtol,
                    ymax=specoptima$lgopt+specoptima$lgtol), width=0)+
  geom_errorbar(aes(x=specoptima$Species,
                    ymin=specoptima$regopt-specoptima$regtol,
                    ymax=specoptima$regopt+specoptima$regtol), width=0)

Output:

enter image description here

Upvotes: 1

Related Questions