【问题标题】:Error in FUN(X[[i]], ...) : object 'Value' not found - NOT a spelling errorFUN(X[[i]], ...) 中的错误:找不到对象“值” - 不是拼写错误
【发布时间】:2021-02-06 00:23:37
【问题描述】:

我正在制作直方图,并尝试向其添加水平误差条。

这是graph.qc的头部

 spec     Value Gain
1 CA  0.3649156  H.P
2 CA  0.3585836  H.P
3 CA  0.2731337  H.P
4 CA  0.3556054  H.P
5 CA  0.3294501  H.P
6 CA  0.3465670  H.P

这是graph.compare的头

 Gain  spec       mean   median     n     sd    SE_H    ymin    ymax
  <chr> <fct>     <dbl>    <dbl> <int>  <dbl>   <dbl>   <dbl>   <dbl>
1 H.NP  CA  -0.0333   -0.0310     70 0.0174 0.00208 -0.0507 -0.0159
2 H.NP  CAF    0.0324    0.0193     54 0.0530 0.00721 -0.0206  0.0853
3 H.NP  CAH    0.0490    0.0503     20 0.0698 0.0156  -0.0208  0.119 
4 H.NP  CAL    0.000585 -0.00805    28 0.0468 0.00884 -0.0462  0.0473
5 H.NP  CAM    0.00288  -0.0125     72 0.0480 0.00566 -0.0451  0.0509
6 H.NP  CAR    0.0211    0.00845    90 0.0383 0.00404 -0.0172  0.0595

这是我的图表代码

注意:只有在我不包含 geom_errorbarh 时才会出现错误

  geom_histogram(position = "identity", alpha = 0.60, bins = 50)+
  geom_density(alpha=0.3)+
  geom_vline(data= graph.compare,  aes(xintercept = mean), color = "red", linetype = "dashed")+
  geom_vline(data=graph.compare, aes(xintercept = median), color="blue", linetype="dashed")+
  geom_errorbarh(data = graph.compare, aes(xmin = ymin, xmax = ymax), width =0.2)+
  facet_wrap(spec~., scales="free", ncol=3)+
  newtheme+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom")

当我运行它时,我得到了错误

Error in FUN(X[[i]], ...) : object 'Value' not found

不知道发生了什么,我查阅了一些信息,发现我可能需要传播和重新收集我的数据,但我真的不明白为什么。同样,如果我只是注释掉 geom_errorbarh 这不是问题,其他所有内容(包括 geom_vline)都会生成没有问题的图表。

感谢您的帮助!

【问题讨论】:

  • 你能显示完整的情节代码吗

标签: r ggplot2


【解决方案1】:

虽然代码不完整,但使用您共享的数据片段可以重现错误。由于graph.compare 中缺少变量Value(该函数正在寻找图中定义的初始坐标)而产生错误,并且您还需要为错误栏设置y 坐标。在这里,我在提到的数据集中以手工方式为Value 添加了一个可能的值,但是使用您的真实数据,您应该能够正确添加它。还对geom_errorbarh() 添加了y 进行了细微更改。接下来是代码:

library(ggplot2)
#Add variable Value
graph.compare$Value <- c(0.15,0.025,0.048,-0.004,-0.005,0.015)
#Code
ggplot(graph.qc,aes(x=Value))+
  geom_histogram(position = "identity", alpha = 0.60, bins = 50)+
  geom_density(alpha=0.3)+
  geom_vline(data= graph.compare,  aes(xintercept = mean), color = "red", linetype = "dashed")+
  geom_vline(data=graph.compare, aes(xintercept = median), color="blue", linetype="dashed")+
  geom_errorbarh(data = graph.compare, aes(xmin = ymin, xmax = ymax,y=5), width =0.2)+
  facet_wrap(spec~., scales="free", ncol=3)+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom")
 

输出:

另外,为了示例,我使用graph.qc 作为情节草图。

使用的一些数据:

#Data 1
graph.qc <- structure(list(spec = c("CA", "CA", "CA", "CA", "CA", "CA"), 
    Value = c(0.3649156, 0.3585836, 0.2731337, 0.3556054, 0.3294501, 
    0.346567), Gain = c("H.P", "H.P", "H.P", "H.P", "H.P", "H.P"
    )), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5", "6"))

#Data 2
graph.compare <- structure(list(Gain = c("H.NP", "H.NP", "H.NP", "H.NP", "H.NP", 
"H.NP"), spec = c("CA", "CAF", "CAH", "CAL", "CAM", "CAR"), mean = c(-0.0333, 
0.0324, 0.049, 0.000585, 0.00288, 0.0211), median = c(-0.031, 
0.0193, 0.0503, -0.00805, -0.0125, 0.00845), n = c(70L, 54L, 
20L, 28L, 72L, 90L), sd = c(0.0174, 0.053, 0.0698, 0.0468, 0.048, 
0.0383), SE_H = c(0.00208, 0.00721, 0.0156, 0.00884, 0.00566, 
0.00404), ymin = c(-0.0507, -0.0206, -0.0208, -0.0462, -0.0451, 
-0.0172), ymax = c(-0.0159, 0.0853, 0.119, 0.0473, 0.0509, 0.0595
)), row.names = c(NA, -6L), class = "data.frame")

【讨论】: