我想获得一条线,该线通过给定点并为指定的时间间隔内的值指定颜色。但我只能得到指定颜色的多行,而不是单行,这会改变其子区间的颜色。geom_line的条件颜色
的可重复的例子如下:
require(ggplot2)
require(plotly)
vectX <- c(-5,-4.5,-3.2,-2.1,-0.8,0.1,1.3,2.7,3.6,4.4,5)
vectY <- c(-2.3,1.4,2.7,0.3,-0.4,1.5,3.9,2.4,0.5,-1.2,1.4)
requestedQuantilesZscores <- c(0.0,0.25,0.5,0.75,1.0)
zScores <- base::scale(vectY, center = TRUE, scale = TRUE)
quantilesZscore <- stats::quantile(zScores, requestedQuantilesZscores, na.rm = TRUE)
theDataFrame <- base::data.frame(theX = vectX, theY = vectY, theZ = zScores)
valuesColor <- c('green','red','blue','yellow','orange')
theDataFrame$conditionalColor <- ifelse(theDataFrame$theZ > quantilesZscore[[4]], valuesColor[[1]] ,
ifelse(theDataFrame$theZ > quantilesZscore[[3]] & theDataFrame$theZ <= quantilesZscore[[4]], valuesColor[[2]],
ifelse(theDataFrame$theZ > quantilesZscore[[2]] & theDataFrame$theZ <= quantilesZscore[[3]], valuesColor[[3]],
ifelse(theDataFrame$theZ <= quantilesZscore[[2]], valuesColor[[4]], valuesColor[[5]]))))
theGGplotLine <- ggplot(theDataFrame) +
geom_line(aes(x = theX, y = theY, color = conditionalColor)) +
xlab('X') + ylab('Y') +
scale_colour_manual(values = valuesColor) +
theme(legend.position='none')
theGGplotLine
(plotly::ggplotly(theGGplotLine))
如果我理解你的问题(我不能肯定我做的),那么你正在寻找的是细分一行成各种颜色。根据我所知,使用'geom_line'无法达到这个目的,您需要恢复到像geom_abline这样的图形原语和朋友分别绘制每条线。 –