问题描述
使用 ggplot2 的 stat_smooth(),我很好奇如何调整生成的回归线的透明度.使用 geom_points() 或 geom_line(),通常为alpha"设置一个值,表示百分比透明度.但是,使用 stat_smooth() 时,alpha 设置了置信区间的透明度(在我下面的示例中,关闭 - se=FALSE).
Using ggplot2's stat_smooth(), I'm curious how one might adjust the transparency of the generated regression line. Using geom_points() or geom_line(), one normally sets a value for 'alpha', indicating the percent transparency. However, with stat_smooth(), alpha sets the transparency of the confidence interval (in my sample below, turned off - se=FALSE).
我似乎无法找到一种方法使回归线的透明度低于 1.
I cannot seem to find a way to make the regression line(s) a lower transparency than 1.
你的建议会很棒.
示例代码
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)
推荐答案
要仅为该行设置 alpha 值,您应该将 stat_smooth()
替换为 geom_line()
和然后在 geom_line()
中使用与 stat_smooth()
中相同的参数并另外添加 stat="smooth"
.
To set alpha value just for the line you should replace stat_smooth()
with geom_line()
and then inside the geom_line()
use the same arguments as in stat_smooth()
and additionally add stat="smooth"
.
ggplot(df, aes(x=x, y=value, color=variable)) +
geom_point(size=2) +
geom_line(stat="smooth",method = "lm", formula = y ~ 0 + I(1/x) + I((x-1)/x),
size = 1.5,
linetype ="dashed",
alpha = 0.5)
这篇关于调整 stat_smooth 线的透明度 (alpha),而不仅仅是 Confidence Interval 的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!