首页 > 解决方案 > 错误:美学必须是长度 1 或与数据 (13) 相同:ymax

问题描述

我正在尝试绘制一个图表,显示三种不同模型 wap、lg 和 reg 的物种最优和公差,但是我不断收到错误,Error: Aesthetics must be either length 1 or the same as the data (13): ymax我读到它与填充有关,但我确定我已经正确定义了填充?我还尝试只保留所有三个模型的共同物种,以便数据框中没有任何 NA,但我得到了同样的错误。任何人都可以解决这个问题吗?



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")
  
  

标签: rggplot2

解决方案


就像鸭子说的那样,你有几个错别字。但这就是为什么你有几个错别字——代码中不必要的重复。您必须输入数据框名称的次数越多,您就越有可能出现拼写错误。冗长而笨重的代码行以及运算符之间的最小间距也使得这些代码更难看到。

您的 geom 都可以从调用中继承它们的数据ggplot,并且您不需要specoptima$variable在 geom 或 stat 调用中执行此操作,因此您的代码简化为:

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()

在此处输入图像描述

或者更好的是,重塑您的数据以避免重复的 geom 调用:

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")

推荐阅读