2013-01-24 35 views
2

使用GGPLOT2我想畫的是在特定日期之後改變顏色的線時間序列數據。我認爲這很簡單,但在顏色改變的地方,我得到了一個突破。起初,我認爲這是與group問題(按this問題; this other question也看了相關的,但是是我需要不大的)。經過與group審美混亂周圍30分鐘,我不能修復它,如果任何人都可以指出明顯的錯誤...R:防止換行符顯示使用ggplot geom_line

screenshot

代碼:

require(ggplot2) 

set.seed(1111) 
mydf <- data.frame(mydate = seq(as.Date('2013-01-01'), by = 'day', length.out = 10), 
    y = runif(10, 100, 200)) 
mydf$cond <- ifelse(mydf$mydate > '2013-01-05', "red", "blue") 

ggplot(mydf, aes(x = mydate, y = y, colour = cond)) + 
    geom_line() + 
    scale_colour_identity(mydf$cond) + 
    theme() 

回答

3

如果設置group=1 ,那麼1將被用作所有數據點的組值,並且該線將加入。

ggplot(mydf, aes(x = mydate, y = y, colour = cond, group=1)) + 
    geom_line() + 
    scale_colour_identity(mydf$cond) + 
    theme() 
+0

呸!已經搜查了我自己的代碼庫,我已經意識到我已經使用這一招之前,但不久前,我忘了!感謝您的提醒。 – SlowLearner