调整stat_smooth行的透明度(alpha),而不仅仅是置信区间的透明度 [英] Adjust Transparency (alpha) of stat_smooth lines, not just transparency of Confidence Interval

查看:997
本文介绍了调整stat_smooth行的透明度(alpha),而不仅仅是置信区间的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot2的stat_smooth(),我很好奇如何调整生成的回归行的透明度。使用geom_points()或geom_line(),通常会为alpha设置一个值,表示透明度百分比。但是,使用stat_smooth(),alpha设置置信区间的透明度(在我的示例中,关闭 - se = FALSE)。

我似乎无法找到让回归线的透明度比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 = $ 0 + I(1 / x)+ I((x-1)/ x),
se = FALSE,
size = 1.5,
alpha = 0.5)

解决方案

要为行设置alpha值,您应该将 stat_smooth() with geom_line(),然后在 geom_line()使用与 stat_smooth()相同的参数,并添加 stat =smooth


$ b $

  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)


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).

I cannot seem to find a way to make the regression line(s) a lower transparency than 1.

Your advice would be wonderful.

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)

解决方案

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),而不仅仅是置信区间的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆