Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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 将abline图例添加到ggplot geom_点_R_Ggplot2_Legend - Fatal编程技术网

R 将abline图例添加到ggplot geom_点

R 将abline图例添加到ggplot geom_点,r,ggplot2,legend,R,Ggplot2,Legend,我想添加一个图例,它只描述我的绘图中的线条,与此类似: library(ggplot) df<-as.data.frame(cbind(seq(1:10), seq(from=2, to=20, 2))) ggplot(data=df, aes(V1, V2))+geom_point()+ geom_abline(intercept=0, slope=1) 我只想把abline添加到图例中。我已经尝试使用geom_线,但当我调整绘图的x和ylims时,它消失了。ggplot2没有从

我想添加一个图例,它只描述我的绘图中的线条,与此类似:

library(ggplot)

df<-as.data.frame(cbind(seq(1:10), seq(from=2, to=20, 2)))
ggplot(data=df, aes(V1, V2))+geom_point()+
  geom_abline(intercept=0, slope=1)

我只想把abline添加到图例中。我已经尝试使用geom_线,但当我调整绘图的x和ylims时,它消失了。ggplot2没有从头开始创建类似于lattice的图例的选项,是吗?

您可以将几何图形参数包装在aes中并添加颜色元素,然后在scale\u color\u手动调用中进行控制:


或者,您可以对两种不同的颜色进行选择:

ggplot(data = df, aes(color="red", V1, V2)) + geom_point() + 
  geom_abline(aes(intercept = 0, slope = 1, color="blue")) + 
  scale_color_manual(values = c("red", "blue")) +
  labs(color=NULL)

谢谢你,现在我的传奇中有了这一行。但是,现在我的传奇中有三行;一个用于abline,两个用于数据点组。是否可以将数据点的行转换为所选的pch?在我的例子16中,我通过使用网格添加一个完全定制的图例来解决这个问题。
ggplot(data = df, aes(color="red", V1, V2)) + geom_point() + 
  geom_abline(aes(intercept = 0, slope = 1, color="blue")) + 
  scale_color_manual(values = c("red", "blue")) +
  labs(color=NULL)