将图例添加到 ggplot2 散点图,包括附加线
Add a legend to a ggplot2 scatter plot including additional lines
我想向 ggplot2
散点图添加图例,以区分回归线和我添加的单独线。
例如,
library(ggplot2)
set.seed(123)
data1=rnorm(1000,1,2)
data2=rnorm(1000,1,4)
DF=data.frame(data1,data2)
ggplot(DF,aes(data1,data2))+geom_point(colour="dodgerblue",alpha=0.75)+geom_smooth(method=lm,se=F,aes(colour="Line of best fit"))+
geom_abline(intercept = 0, slope = 1, linetype="dashed", colour="black", alpha=1,size=1)
此图上有两条线,一条是红色回归线,一条是带有等式 y=x
的黑线。
我已经设法在图例中添加回归线,但想添加黑线。作为旁注,我也希望能够将图例的名称从 colour
.
更改为
可能有更简单的解决方案,但这是目前我能想到的最好的解决方案。
ggplot(DF, aes(data1,data2)) +
geom_point(colour="dodgerblue",alpha=0.75) +
geom_abline(aes(colour="abline", intercept=0, slope=1), linetype="dashed", alpha=1, size=1) +
geom_smooth(aes(colour="lm_smooth"), method = "lm", se=FALSE) +
scale_colour_manual(name="lines", values=c("red", "blue")) +
guides(colour = guide_legend(override.aes = list(alpha = 0)))
功劳也有 here.
我想向 ggplot2
散点图添加图例,以区分回归线和我添加的单独线。
例如,
library(ggplot2)
set.seed(123)
data1=rnorm(1000,1,2)
data2=rnorm(1000,1,4)
DF=data.frame(data1,data2)
ggplot(DF,aes(data1,data2))+geom_point(colour="dodgerblue",alpha=0.75)+geom_smooth(method=lm,se=F,aes(colour="Line of best fit"))+
geom_abline(intercept = 0, slope = 1, linetype="dashed", colour="black", alpha=1,size=1)
此图上有两条线,一条是红色回归线,一条是带有等式 y=x
的黑线。
我已经设法在图例中添加回归线,但想添加黑线。作为旁注,我也希望能够将图例的名称从 colour
.
可能有更简单的解决方案,但这是目前我能想到的最好的解决方案。
ggplot(DF, aes(data1,data2)) +
geom_point(colour="dodgerblue",alpha=0.75) +
geom_abline(aes(colour="abline", intercept=0, slope=1), linetype="dashed", alpha=1, size=1) +
geom_smooth(aes(colour="lm_smooth"), method = "lm", se=FALSE) +
scale_colour_manual(name="lines", values=c("red", "blue")) +
guides(colour = guide_legend(override.aes = list(alpha = 0)))
功劳也有 here.