在for循环中运行ggplot时,二进制运算符(errorbar)的非数字参数
当我尝试添加相应的errorbars时,我一直在尝试在for循环中创建ggplots 要复制错误的数据帧:在for循环中运行ggplot时,二进制运算符(errorbar)的非数字参数,r,ggplot2,errorbar,R,Ggplot2,Errorbar,当我尝试添加相应的errorbars时,我一直在尝试在for循环中创建ggplots 要复制错误的数据帧: FINAL_BIRDS=structure(list(Treatment = c("erythrocytes", "erythrocytes", "steroid", "steroid","saturated_carbohydrate", "saturated_carbohydrate", "unsaturated_carbohydrate", "unsaturated_carbohydr
FINAL_BIRDS=structure(list(Treatment = c("erythrocytes", "erythrocytes", "steroid", "steroid","saturated_carbohydrate", "saturated_carbohydrate", "unsaturated_carbohydrate", "unsaturated_carbohydrate", "control"),
Sleep = c(FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE),
stderr_area. = c(25.2419795929641, 14.9130357358005, 26.8118518761557, 10.0726607683124, 31.2843207910337,
12.1257884222765, 25.6371591089567, 19.2819746866121, 25.0891862104105),
stderr_convex_hull_area. = c(47.2925074176382, 54.9644641679047,47.902530313873, 38.4682148026727, 59.235691895529,
13.0445638906346, 34.1646485413064, 64.3496918069692, 46.3824137518226),
stderr_solidity. = c(0.0173055524483047, 0.0299738382839415, 0.0111109163382894, 0.0256906757514238, 0.0383184031467805,
0.0201101581307241, 0.0317966622671069, 0.026641685951252, 0.0175635694617109),
mean_area. = c(281.206098616, 307.847857263202, 289.652372512966, 313.926158961588, 228.580981650845,
277.434083621289, 212.823732276778, 268.723306954329, 261.635619956637),
mean_convex_hull_area. = c(503.997742769278, 583.752222613032,532.459453592992, 538.54560438105,
425.028542307996, 467.447197721035,411.644305637731, 523.653759424016, 484.365635850339),
mean_solidity. = c(0.555418485236562,0.535021867782228, 0.542686645447222, 0.588389258540302, 0.583747393534253,
0.593427068141056, 0.505346818720391, 0.516443711837328, 0.527916732368975)),
.Names = c("Treatment", "Sleep", "stderr_area.", "stderr_convex_hull_area.", "stderr_solidity.",
"mean_area.","mean_convex_hull_area.","mean_solidity."), row.names = c(NA, -9L), class = "data.frame")
我已经列出了在forloop中调用我的列标题的列表:
variables <- c("mean_area.","mean_convex_hull_area.","mean_solidity.")
stderror<- c("stderr_area.","stderr_convex_hull_area.","stderr_solidity.")
perfect<-mapply(c, variables, stderror, SIMPLIFY=FALSE)
不幸的是,我遇到了这个错误,但无法找出原因:
Error in perfect[[i]][1] - perfect[[i]][2] :
non-numeric argument to binary operator
然而,我确实知道代码正在工作,因为当我将stderror绘制为ymin,mean绘制为ymax时,它显示了正确的值。似乎只有减法和/或加法会引起问题
plot_list=list()
for(i in 1:length(perfect)){
p= ggplot(data=FINAL_BIRDS, aes_string(x="Treatment",y=perfect[[i]][1], fill="Sleep"))+
geom_bar(stat="identity", position=position_dodge())+
theme(axis.text.x = element_text(angle = 270, hjust = 1))+
geom_errorbar(aes_string(ymin=perfect[[i]][2], ymax=perfect[[i]][1]),
width=.2, position=position_dodge(.9))
file_name = paste(paste(getwd()),"/",perfect[[i]][1], ".tiff", sep="")
tiff(file_name)
print(p)
dev.off()
while (!is.null(dev.list())) dev.off()
}
我得到的工作结果如下,表明它知道我的标准误差和平均值是多少
您不能在
aes\u字符串中执行此操作:
perfect[[i]][1]-perfect[[i]][2]
这将首先评估为
"mean_area." - "stderr_area."
它将尝试减去两个字符串,并产生一个错误
您需要将整个表达式构造为字符串,例如:
paste(perfect[[i]][1], '-', perfect[[i]][2])
请使您的代码独立,并检查是否存在印刷错误<在您的示例中,没有在任何地方创建代码>最终鸟
。所以我们不能运行它。此外,您的geom\u errorbar()
没有足够的右括号。目前,我们无法运行您的示例来重新创建错误。谢谢Cymon,我已经对问题进行了适当的更改。
paste(perfect[[i]][1], '-', perfect[[i]][2])