如何在ggplot2中绘制两条线
这似乎与 Hadley 的 ggplot2 书中的一些示例类似,但我似乎无法完成这项工作。 给定:
off = c(0, 2000, 4000, 6000, 25, 3000, 6050, 9000)
tim = c( 0, -100, -200, -300 -25, -125, -225, -325)
col = c( 1, 1, 1, 1, 2, 2, 2, 2)
dataf = data.frame(off, tim, col)
p = ggplot(dataf, aes(off, tim, color=col)) + geom_point() + geom_line()
p
我认为这应该绘制这八个点,并用 col = 1 绘制一条穿过前四个点的线,用 col = 2 绘制另一条穿过最后四个点的线。然而我最终得到的是一条红色和蓝色交替的线段。
为什么?!
This seems to be a similar example to some of Hadley's examples in his ggplot2 book, but I can't seem to make this work.
Given:
off = c(0, 2000, 4000, 6000, 25, 3000, 6050, 9000)
tim = c( 0, -100, -200, -300 -25, -125, -225, -325)
col = c( 1, 1, 1, 1, 2, 2, 2, 2)
dataf = data.frame(off, tim, col)
p = ggplot(dataf, aes(off, tim, color=col)) + geom_point() + geom_line()
p
I think this should plot these eight points and draw ONE line through the first four points with col = 1 and another line through the last four points with col = 2. Yet what I end up with is one line with alternating red and blue segments.
Why?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
col
是数字。分组设置为因子变量的交互作用,但由于没有因子变量,因此将线绘制为单个组。您可以将col
更改为一个因子,或手动设置分组
Because
col
is numeric. Grouping is set to the interaction of factor variables, but since there are none the line is plotted as a single group. You can either changecol
to a factor,or manually set the grouping