对ggplot2中的分组点感到困惑

时间:2015-04-27 13:03:10

标签: r ggplot2

为什么这段代码没有在y的相同值的数据之间划线?

main <- data_frame(x=rep(c(-1, 1), each=2), y = c(c(1, 1), c(2, 2)), z = c(1, 2, 3, 4))
qplot(data = main, x = x, y = z, geom="line", group=factor(y))

这是我得到的:

original

但我只希望连接y级别的点。

grouping I want

1 个答案:

答案 0 :(得分:4)

The issue is with how you defined your y variable. Change it to y = c(c(1,2), c(1,2)) and things should work.

Also, if you're going to use data_frame be sure to add the calls to library to make your code reproducible (i.e., library(dplyr) and library(ggplot2)).