当 运行 ggplot 在 for 循环中时,non-numeric 二元运算符(错误栏)的参数
non-numeric argument to binary operator (errorbar) when running ggplot in a for loop
当我尝试添加相应的错误栏时,我无法尝试在 for 循环中创建 ggplots
重现错误的数据框:
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 中调用我的专栏 headers:
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)
for 循环:
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]][1]-perfect[[i]][2], ymax=perfect[[i]][1]+perfect[[i]][2]),
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()
}
不幸的是,我 运行 遇到了这个错误,但无法弄清楚原因:
Error in perfect[[i]][1] - perfect[[i]][2] :
non-numeric argument to binary operator
但是,我知道代码可以正常工作,因为当我将标准错误绘制为 ymin 并将平均值绘制为 ymax 时,它显示了正确的值。只有减法 and/or 加法似乎会导致问题。
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()
}
我得到如下工作结果,表明它知道我的 stderror 和 mean 的值是什么。
您不能在 aes_string
内执行此操作:
perfect[[i]][1]-perfect[[i]][2]
这将首先评估为
"mean_area." - "stderr_area."
它将尝试减去两个字符串,并产生错误。
您需要将整个表达式构造为字符串,例如:
paste(perfect[[i]][1], '-', perfect[[i]][2])
当我尝试添加相应的错误栏时,我无法尝试在 for 循环中创建 ggplots
重现错误的数据框:
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 中调用我的专栏 headers:
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)
for 循环:
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]][1]-perfect[[i]][2], ymax=perfect[[i]][1]+perfect[[i]][2]),
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()
}
不幸的是,我 运行 遇到了这个错误,但无法弄清楚原因:
Error in perfect[[i]][1] - perfect[[i]][2] :
non-numeric argument to binary operator
但是,我知道代码可以正常工作,因为当我将标准错误绘制为 ymin 并将平均值绘制为 ymax 时,它显示了正确的值。只有减法 and/or 加法似乎会导致问题。
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()
}
我得到如下工作结果,表明它知道我的 stderror 和 mean 的值是什么。
您不能在 aes_string
内执行此操作:
perfect[[i]][1]-perfect[[i]][2]
这将首先评估为
"mean_area." - "stderr_area."
它将尝试减去两个字符串,并产生错误。
您需要将整个表达式构造为字符串,例如:
paste(perfect[[i]][1], '-', perfect[[i]][2])