使用ggplot2的stat_smooth(),我很好奇如何调整生成的回归线的透明度.使用geom_points()或geom_line(),通常会设置"alpha"的值,表示透明度的百分比.然而,使用stat_smooth(),alpha设置置信区间的透明度(在下面的示例中,关闭-se=FALSE).
我似乎找不到办法使回归线的透明度低于1.
你的建议太好了.
Sample Code
library(reshape2)
df <- data.frame(x = 1:300)
df$y1 <- 0.5*(1/df$x + 0.1*(df$x-1)/df$x + rnorm(300,0,0.015))
df$y2 <- 0.5*(1/df$x + 0.3*(df$x-1)/df$x + rnorm(300,0,0.015))
df$y3 <- 0.5*(1/df$x + 0.6*(df$x-1)/df$x + rnorm(300,0,0.015))
df <- melt(df, id = 1)
ggplot(df, aes(x=x, y=value, color=variable)) +
geom_point(size=2) +
stat_smooth(method = "lm", formula = y ~ 0 + I(1/x) + I((x-1)/x),
se = FALSE,
size = 1.5,
alpha = 0.5)