我正在使用ggplot绘制以下图形(随附一个示例)。我要实现的是让一个图例显示组合为“组1”的线型4和形状1,以及组合为“组2”的线型1和形状2。另外,图例名称将是“示例图例”之类的名称(基于df1,df2:分为组1和组2,每个组有3行)
我尝试过scale_shape_manual和scale_linetype_manual。但是,我没有找到正确的方法。代码如下所示(图中没有要正确添加的图例)
谢谢。
y1 <- x + 0.01
y2 <- x + 10
df1 <- data.frame(x, y1)
df2 <- data.frame(x, y2)
graph_1 <- subset(df1, x >= 0 & x <= 5)
graph_2 <- subset(df1, x >= 6 & x <= 10)
graph_3 <- subset(df1, x >= 11 & x <= 15)
graph_4 <- subset(df2, x >= 0 & x <= 5)
graph_5 <- subset(df2, x >= 6 & x <= 10)
graph_6 <- subset(df2, x >= 11 & x <= 15)
win.graph(width = 13, height = 6, pointsize = 8)
ggplot() +
geom_point(aes(x, y1), data = graph_1, shape = 1) +
geom_smooth(aes(x, y1), data = graph_1, method = "loess", linetype = 4) +
geom_point(aes(x, y1), data = graph_2, shape = 1) +
geom_smooth(aes(x, y1), data = graph_2, method = "loess", linetype = 4) +
geom_point(aes(x, y1), data = graph_3, shape = 1) +
geom_smooth(aes(x, y1), data = graph_3, method = "loess", linetype = 4) +
geom_point(aes(x, y2), data = graph_4, shape = 2) +
geom_smooth(aes(x, y2), data = graph_4, method = "loess", linetype = 1) +
geom_point(aes(x, y2), data = graph_5, shape = 2) +
geom_smooth(aes(x, y2), data = graph_5, method = "loess", linetype = 1) +
geom_point(aes(x, y2), data = graph_6, shape = 2) +
geom_smooth(aes(x, y2), data = graph_6, method = "loess", linetype = 1)