2017-10-09 95 views
0

我想獲得一條線,該線通過給定點併爲指定的時間間隔內的值指定顏色。但我只能得到指定顏色的多行,而不是單行,這會改變其子區間的顏色。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)) 
+0

如果我理解你的問題(我不能肯定我做的),那麼你正在尋找的是細分一行成各種顏色。根據我所知,使用'geom_line'無法達到這個目的,您需要恢復到像geom_abline這樣的圖形原語和朋友分別繪製每條線。 –

回答

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列。分組這樣也可以基於一個單獨的列,如果有必要

+0

謝謝@ user2738526。你的答案提供了靜態圖(用ggplot2創建)的預期行爲,並且它將被接受。交互式情節(與ggplotly)仍然有一些問題,但你的答案讓我在正確的方向,我也能夠解決它 – Aex

0

答案組合由user2738526了原來的問題提供解決方案的要素,也解決了Question 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))