R 如何有条件地高亮显示ggplot2刻面图中的点-将颜色映射到列

R 如何有条件地高亮显示ggplot2刻面图中的点-将颜色映射到列,r,plot,ggplot2,R,Plot,Ggplot2,在下面的示例中,我创建了两系列点,并使用ggplot2绘制它们。我还根据他们的价值观强调了几点 library(ggplot2) x <- seq(0, 6, .5) y.a <- .1 * x -.1 y.b <- sin(x) df <- data.frame(x=x, y=y.a, case='a') df <- rbind(df, data.frame(x=x, y=y.b, case='b')) print(ggplot(df) + geom_point(

在下面的示例中,我创建了两系列点,并使用
ggplot2
绘制它们。我还根据他们的价值观强调了几点

library(ggplot2)
x <- seq(0, 6, .5)
y.a <- .1 * x -.1
y.b <- sin(x)
df <- data.frame(x=x, y=y.a, case='a')
df <- rbind(df, data.frame(x=x, y=y.b, case='b'))
print(ggplot(df) + geom_point(aes(x, y), color=ifelse(df$y<0, 'red', 'black')))

如何实现这一点?

您应该将
color=ifelse(y而不是使用scale\u…\u identity,您还可以将颜色(和填充)美学包装在
I()
中。这还需要在
aes
中定义颜色

我遇到了这个问题,我想OP可能是无意中使用了
I()

我不确定我是否会利用它,但我觉得这很有趣

库(ggplot2)

谢谢,我不知道比例、颜色、身份。这很强大。
> print(ggplot(df) + geom_point(aes(x, y), color=ifelse(df$y<0, 'red', 'black')) + facet_grid(case ~. ,))
Error: Incompatible lengths for set aesthetics: colour
ggplot(df) + geom_point(aes(x, y, color=ifelse(y<0, 'red', 'black'))) + 
   facet_grid(case ~. ,)+scale_color_identity()