Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2图例:如何组合线型和形状_R_Ggplot2_Legend - Fatal编程技术网

R ggplot2图例:如何组合线型和形状

R ggplot2图例:如何组合线型和形状,r,ggplot2,legend,R,Ggplot2,Legend,我使用ggplot绘制以下图表(附一个示例)。我想要实现的是有一个图例来显示线型4和形状1组合为“组1”,线型1和形状2组合为“组2”。此外,图例名称将是“示例图例”(基于df1,df2:分为组1和组2,每组有3行) 我试过缩放、形状和线型。然而,我没有找到正确的方法。代码如下所示(图中没有我要正确添加的图例) 谢谢 y1 <- x + 0.01 y2 <- x + 10 df1 <- data.frame(x, y1) df2 <- data.frame(x, y2)

我使用ggplot绘制以下图表(附一个示例)。我想要实现的是有一个图例来显示线型4和形状1组合为“组1”,线型1和形状2组合为“组2”。此外,图例名称将是“示例图例”(基于df1,df2:分为组1和组2,每组有3行)

我试过缩放、形状和线型。然而,我没有找到正确的方法。代码如下所示(图中没有我要正确添加的图例)

谢谢

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)

y1类似的内容?请使用
dput()
(而不是
str
head
或图片/屏幕截图)共享您的数据样本,以便其他人可以提供帮助。在这里看到更多是的,很相似。但有点不同:我想要两个传说,例如“第一组”和“第二组”。每个组有3条线,还考虑将数据转换成长格式,以使绘图更容易,而无需手动键入每一个数据帧。请参见以下示例:,如上所述,ggplot需要长格式的数据。然后,您会发现为数据子集指定形状和线型要容易得多。考虑重塑数据集,使每个变量都是一列,每行都是一个观察值。