R 如何在不同的失败率下保持ggplot中的一致着色?

R 如何在不同的失败率下保持ggplot中的一致着色?,r,ggplot2,R,Ggplot2,我有一个这样的数据帧 Datetime <- c("2015-09-29 08:22:00", "2015-09-29 09:45:00", "2015-09-29 09:53:00", "2015-09-29 10:22:00", "2015-09-29 10:42:00", "2015-09-29 11:31:00", "2015-09-29 11:47:00", "2015-09-29 12:45:00", "2015-09-29 13:11:00", "

我有一个这样的数据帧

Datetime <- c("2015-09-29 08:22:00", "2015-09-29 09:45:00", "2015-09-29 09:53:00", "2015-09-29 10:22:00", "2015-09-29 10:42:00",
              "2015-09-29 11:31:00", "2015-09-29 11:47:00", "2015-09-29 12:45:00", "2015-09-29 13:11:00", "2015-09-29 13:44:00",
              "2015-09-29 15:24:00", "2015-09-29 16:28:00", "2015-09-29 20:22:00", "2015-09-29 21:38:00", "2015-09-29 23:34:00")

FailRate <- c(7,4,5.5,6,13,7.6,6.4,15.5,4.6,13.3,9.7,8.4,5.1,1.6,1.3)

df <- data.frame(Datetime,FailRate)
df$Datetime <- as.POSIXlt(df$Datetime)
我怎样才能像这样定制它

我试图包括
比例\u颜色\u渐变(low=“red”)
,但它给了我一个错误

有人能提供一些指导,让这个工作

添加

 scale_colour_manual(values = c("TRUE" = "red","FALSE" = "blue"))

应该这样做。

您应该尝试创建一个包含条件语句的调色板:

pal = ifelse(FailRate< 21,'green','red')
通过这种方式设置代码,您可以根据正在评估的条件动态设置颜色。 如果需要,您甚至可以通过某种方式将其设置为自动调整:

newRate<- max(oldRate)*.5 #any equation here would work function creating  thresholds dynamically.

pal <- ifelse(FailRate< newRate,'green','red')
newRate
 scale_colour_manual(values = c("TRUE" = "red","FALSE" = "blue"))
pal = ifelse(FailRate< 21,'green','red')
  geom_point(alpha=0.6, position=position_jitter(w=0.05, h=0.0), aes(colour=pal), size=3) +
newRate<- max(oldRate)*.5 #any equation here would work function creating  thresholds dynamically.

pal <- ifelse(FailRate< newRate,'green','red')