2017-04-10 109 views
2

我有一個數據框,我用geom_lineggplot2畫一條線(見圖1)。如何使用ggplot2繪製類別變量的線?

data = read.csv('data.csv') 
ggplot() + geom_line(aes(x=1:10,y=data$FA[1:10]),size=I(1.5)) 

    FA  FT 
1 1.07644 0 
2 1.07611 0 
3 1.07462 1 
4 1.07328 0 
5 1.06994 0 
6 1.07026 1 
7 1.06879 0 
8 1.06815 1 
9 1.06979 0 
10 1.07243 1 

如何添加另一條線在那裏FT == 1現有的情節,因此看起來會在第二張照片?
(連接點,其中FT == 1

enter image description here

enter image description here

回答

3

您可以通過添加分組FT線,並設置與FT == 0到NA線的顏色。

ggplot(dat) + 
    geom_line(aes(x = 1:10,y = FA[1:10]), size = I(1.5)) + 
    geom_line(aes(x = 1:10,y = FA[1:10], group = FT, colour = factor(FT)), size = I(1.5), show.legend = FALSE) + 
    scale_colour_manual(values = c(NA, "red")) 

enter image description here