ggplot2:用线显示一组,用点显示另一组

ggplot2: display one group with a line and another group with points

我有一个这样的矩阵:

    names        x        y    group
1       1   -0.050    -0.76        2
2       2    0.040    -0.36        2
3       3    0.060    -0.28        2
4       4    0.080    -0.22        1
5       5    0.080    -0.14        1
6       6    0.040    -0.26        1
7       7    0.030    -0.36        1

我正在尝试用 ggplot2 制作图表,但我想用线表示一组,用点表示另一组。我该怎么做?

你可以试试这个

ggplot(df, aes(x, y))+
geom_point(data=df[df$group==2, ])+
geom_line(data=df[df$group==1, ])

数据

df <- dput(df)
structure(list(names = 1:7, x = c(-0.05, 0.04, 0.06, 0.08, 0.08, 
0.04, 0.03), y = c(-0.76, -0.36, -0.28, -0.22, -0.14, -0.26, 
-0.36), group = c(2L, 2L, 2L, 1L, 1L, 1L, 1L)), .Names = c("names", 
"x", "y", "group"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7"))