alpha for stat_smooth line (not just CI)

1,504 views
Skip to first unread message

Allan

unread,
Mar 6, 2010, 3:34:00 PM3/6/10
to ggplot2
Hi,
I would like to use alpha transparency on the line returned by
stat_smooth (not just the confidence interval). I am trying to show
many smoothers after resampling. The colour and size parameters apply
to the line but alpha only applies to the confidence interval. Is
there a way to modify the layer object returned by a call to
stat_smooth?
Thanks,
Allan

p <- ggplot(mtcars)
for (i in 1:3){
bootdata <- mtcars[sample(1:dim(mtcars)[1], replace = T), ]
p <- p + stat_smooth(data = bootdata, aes(qsec, wt), se = F, size =
2, alpha = .2)
}
print(p)

Xie Chao

unread,
Mar 6, 2010, 10:29:52 PM3/6/10
to Allan, ggplot2
ggplot(mtcars, aes(qsec, wt)) +
geom_line(stat='smooth', alpha=0.3, size=4)

or, if you need the ribbon, add this before the geom_line:
geom_ribbon(aes(ymin=..ymin.., ymax=..ymax..), stat='smooth', alpha=0.1)

Xie Chao

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

Kevin

unread,
Feb 20, 2013, 3:46:30 PM2/20/13
to ggp...@googlegroups.com, Allan
Is there a newer way to do this? I want to maintain one lm line for all the data rather than for each color group:

p <- ggplot(df, aes(Score, Sales_Totals, color=Region)) + geom_point(alpha=.90) 
p + stat_smooth(method="lm", se=T, col="#D00000",alpha=.25,size=1) + facet_wrap(~Round, nrow=1)

I am trying to lower the opacity of the line and not the se bands. 

Ista Zahn

unread,
Feb 20, 2013, 4:01:14 PM2/20/13
to Kevin, ggp...@googlegroups.com, Allan
On Wed, Feb 20, 2013 at 3:46 PM, Kevin <skycon...@gmail.com> wrote:
> Is there a newer way to do this?

I don't know about "newer", but you can use

stat_smooth(method="lm", se=T, col="#D00000",alpha=.25,size=1)

I want to maintain one lm line for all the
> data rather than for each color group:

If you want to use a color scale on the smoother my solution won't
work (unless you use scale_color_manual()). Not clear if this is
required so I've not addressed it here.

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ista Zahn

unread,
Feb 20, 2013, 4:02:20 PM2/20/13
to Kevin, ggp...@googlegroups.com, Allan
On Wed, Feb 20, 2013 at 4:01 PM, Ista Zahn <ista...@gmail.com> wrote:
> On Wed, Feb 20, 2013 at 3:46 PM, Kevin <skycon...@gmail.com> wrote:
>> Is there a newer way to do this?
>
> I don't know about "newer", but you can use
>
> stat_smooth(method="lm", se=T, col="#D00000",alpha=.25,size=1)

Umm, I meant

geom_smooth(alpha=.2, color=alpha("#D00000", .2))

here, sorry!

-Ista

Kevin

unread,
Feb 20, 2013, 4:44:39 PM2/20/13
to Ista Zahn, ggp...@googlegroups.com, Allan
I tried it but get the following error:
Error in do.call("layer", list(mapping = mapping, data = data, geom = geom,  : 
  could not find function "alpha"

Ista Zahn

unread,
Feb 20, 2013, 4:55:17 PM2/20/13
to Kevin, ggp...@googlegroups.com, Allan
On Wed, Feb 20, 2013 at 4:44 PM, Kevin <skycon...@gmail.com> wrote:
> I tried it but get the following error:
> Error in do.call("layer", list(mapping = mapping, data = data, geom = geom,
> :
> could not find function "alpha"

Which version of ggplot2 do you have? does ?alpha have a help page?

Best,
Ista

Ista Zahn

unread,
Feb 20, 2013, 4:59:38 PM2/20/13
to Kevin, ggp...@googlegroups.com, Allan
On Wed, Feb 20, 2013 at 4:55 PM, Ista Zahn <ista...@gmail.com> wrote:
> On Wed, Feb 20, 2013 at 4:44 PM, Kevin <skycon...@gmail.com> wrote:
>> I tried it but get the following error:
>> Error in do.call("layer", list(mapping = mapping, data = data, geom = geom,
>> :
>> could not find function "alpha"
>
> Which version of ggplot2 do you have? does ?alpha have a help page?

Scratch that, it's in the scales package. Do

library(scales)

before plotting.

Best,
Ista
Reply all
Reply to author
Forward
0 new messages