0

我想得到一条穿过给定点的单线,并为指定间隔内的值指定颜色。但是我只能得到指定颜色的多行,而不是会在其子间隔上改变颜色的单行。

可重现的示例如下:

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))
4

2 回答 2

1

答案结合了 user2738526 为原始问题提供的解决方案元素,以及问题 46146720的解决方案

它使用 geom_segment,并使每个段链接到下一个 (x, y) 值,而不管颜色如何。

代码变成

require(ggplot2)
require(plotly)
require((dplyr))

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)

theDataFrame <- theDataFrame %>%
  arrange(theX) %>%
  mutate(theNextX = lead(theX), theNextY = lead(theY))

valuesColor <- c('green','red','blue','magenta','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, aes(x = theX, y = theY)) +
  geom_segment(aes(xend = theNextX, yend = theNextY, color = conditionalColor)) +
  xlab('X') + ylab('Y') +
  scale_colour_manual(values = valuesColor) +
  theme_bw() +
  theme(legend.position='none')

theGGplotLine

(plotly::ggplotly(theGGplotLine)) 
于 2017-10-10T08:04:40.590 回答
0

如果我正确理解你想要什么,你所需要的就是在你的 ggplot 代码中添加一个不同的分组

theGGplotLine <- ggplot(theDataFrame) +
  geom_line(aes(x = theX, y = theY, color = conditionalColor, group = 1)) +
  xlab('X') + ylab('Y') +
  scale_colour_manual(values = valuesColor) +
  theme(legend.position='none')

这给出了一条连接所有点的线,但颜色取决于 conditionalColor 列。如有必要,这样的分组也可以基于单独的列

于 2017-10-09T23:38:20.233 回答