Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R aes_字符串不能正确计算嵌套的ifelse_R_Ggplot2 - Fatal编程技术网

R aes_字符串不能正确计算嵌套的ifelse

R aes_字符串不能正确计算嵌套的ifelse,r,ggplot2,R,Ggplot2,我使用嵌套的ifelse语句突出显示数据集中的极高/极低值。我想尝试使用aes\u字符串(…,color=…),而不是为色彩美学添加一个额外的列(我知道怎么做),因为这个绘图将进入一个函数(因此是aes\u字符串而不是aes) 下面是一个简单的示例,ggplot2评估嵌套的ifelse语句并错误地着色 library(ggplot2) theme_set(theme_bw()) sample <- data.frame(x = 1:10, y = 1:10) # [Plot 1] Wor

我使用嵌套的
ifelse
语句突出显示数据集中的极高/极低值。我想尝试使用
aes\u字符串(…,color=…)
,而不是为色彩美学添加一个额外的列(我知道怎么做),因为这个绘图将进入一个函数(因此是
aes\u字符串而不是
aes

下面是一个简单的示例,
ggplot2
评估嵌套的
ifelse
语句并错误地着色

library(ggplot2)
theme_set(theme_bw())
sample <- data.frame(x = 1:10, y = 1:10)

# [Plot 1] Works fine using aes and stipulating variables
ggplot(sample, aes(x = x, y = y)) +
  geom_point(aes(colour = ifelse(x < 3, 1, ifelse(x > 7, 10, x)))) +
  scale_colour_identity()

# [Plot 2] "x" replaced by "var" and "aes" replaced by "aes_string"
var = "x"
ggplot(sample, aes(x = x, y = y)) +
  geom_point(aes_string(colour = ifelse(var < 3, 1, ifelse(var > 7, 10, var)))) +
  scale_colour_identity()
库(ggplot2)
theme_set(theme_bw())
样本7、10、x)+
比例\颜色\标识()
#[Plot 2]“x”替换为“var”,而“aes”替换为“aes_字符串”
var=“x”
ggplot(样本,aes(x=x,y=y))+
几何点(aes字符串(颜色=ifelse(var<3,1,ifelse(var>7,10,var)))+
比例\颜色\标识()

[Plot 1]您可以看到,在开始和结束时,颜色分别突出显示为蓝色(1)和红色(10)

[Plot 2]您可以看到,所有点都以红色突出显示(10),表示出现了问题


任何帮助都将不胜感激

这不是一个bug。对于
aes\u字符串
,应明确引用参数。您可以尝试:
var=“x”;aes\u col或
aes\u string(color=“ifelse(get(var)<3,1,ifelse(get(var)>7,10,get(var))”)
。啊,我明白我的错误了!需要将整个字符串传递给
color
,而不仅仅是我想要的位和块。谢谢@mt1022,效果很好@卢卡,如果你把你的评论作为一个答案,我会很高兴地接受它作为解决方案,因为它看起来有点优雅