简体   繁体   English

如何使用ggplot2绘制带有类别变量的线?

[英]How to use ggplot2 to plot lines with category variable?

I have a data frame, I've used geom_line from ggplot2 to draw a line (see picture 1). 我有一个数据框,我使用了geom_lineggplot2画一条线(见图1)。

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

How do I add another line where FT == 1 to the existing plot so it will look something the the second picture? 如何在现有图上添加另一条FT == 1线,以使其看起来像第二张图片?
(connect the points where FT == 1 ) (连接FT == 1的点)

在此处输入图片说明

在此处输入图片说明

You can add a line grouped by FT and set the colour of the line with FT == 0 to NA. 您可以添加按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"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM