Hi R Users, I was trying to create a figure with geom_ribbon. There is a function "fill", but I want to make the shaded area with a gradient (increasing dark color towards a central line, inserted of having a color). Is there any possibility? In the given example, I want the colour with "blue" but in a gradient (dark=central, light= as goes higher or lower) pl = data.frame(Time = 0:10, menle = rnorm(11)) pl$menlelb = pl$menle -1 pl$menleub = pl$menle +1 ggplot(pl, aes(Time)) + geom_line(aes(y=menle), colour="blue") + geom_ribbon(aes(ymin=menlelb, ymax=menleub), fill="blue") Thanks [[alternative HTML version deleted]]
Hi If the ribbon has constant "height" then this can be hacked by drawing a bunch of ribbons (polygons) with different heights and slowly changing colours. If the height of the ribbon varies, then you could use the same approach and clip the result, with a little bit more work. The attached code demonstrates the latter approach (the image files that it produces for me are also attached). Hope that helps Paul On 11/05/17 08:05, Kristi Glover wrote:> Hi R Users, > > I was trying to create a figure with geom_ribbon. There is a function > "fill", but I want to make the shaded area with a gradient > (increasing dark color towards a central line, inserted of having a > color). Is there any possibility? > > > In the given example, I want the colour with "blue" but in a gradient > (dark=central, light= as goes higher or lower) > > > pl = data.frame(Time = 0:10, menle = rnorm(11)) > > pl$menlelb = pl$menle -1 > > pl$menleub = pl$menle +1 > > ggplot(pl, aes(Time)) + > > geom_line(aes(y=menle), colour="blue") + > > geom_ribbon(aes(ymin=menlelb, ymax=menleub), fill="blue") > > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ R-help at r-project.org > mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the > posting guide http://www.R-project.org/posting-guide.html and provide > commented, minimal, self-contained, reproducible code. >-- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/ -------------- next part -------------- A non-text attachment was scrubbed... Name: gradient.pdf Type: application/pdf Size: 8834 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170511/59604882/attachment.pdf> -------------- next part -------------- A non-text attachment was scrubbed... Name: ggplot.pdf Type: application/pdf Size: 4761 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170511/59604882/attachment-0001.pdf>
Hi Kristi,
It can be done, but it is messy:
pl = data.frame(Time = 0:10, menle = rnorm(11))
pl$menlelb = pl$menle -1
pl$menleub = pl$menle +1
rg<-0.95
blue<-1
plot(pl$Time,pl$menlelb,ylim=range(c(pl$menlelb,pl$menleub)),type="l",
lwd=7,col=rgb(rg,rg,blue))
lines(pl$Time,pl$menlelb,lwd=7,col=rgb(rg,rg,blue))
rg<-seq(0.9,0.3,length.out=9)
offset<-seq(0.88,0.08,by=-0.1)
for(i in 1:9) {
lines(pl$Time,pl$menle+offset[i],lwd=7,col=rgb(rg[i],rg[i],blue))
lines(pl$Time,pl$menle-offset[i],lwd=7,col=rgb(rg[i],rg[i],blue))
}
lines(pl$Time,pl$menle,lwd=6,col=rgb(0,0,blue))
For the ggplot solution, this might work:
ggplot(pl, aes(Time)) +
geom_line(aes(y=menle+1), colour=rgb(0.95,0.95,1), width=7) +
geom_line(aes(y=menle-1), colour=rgb(0.95,0.95,1), width=7) +
geom_line(aes(y=menle+0.88), colour=rgb(0.9,0.9,1), width=7) +
geom_line(aes(y=menle-0.88), colour=rgb(0.9,0.9,1), width=7) +
geom_line(aes(y=menle+0.78), colour=rgb(0.825,0.825,1), width=7) +
geom_line(aes(y=menle-0.78), colour=rgb(0.825,0.825,1), width=7) +
geom_line(aes(y=menle+68), colour=rgb(0.75,0.75,1), width=7) +
geom_line(aes(y=menle-68), colour=rgb(0.75,0.75,1), width=7) +
geom_line(aes(y=menle+0.58), colour=rgb(0.675,0.675,1), width=7) +
geom_line(aes(y=menle-0.58), colour=rgb(0.675,0.675,1), width=7) +
geom_line(aes(y=menle+0.48), colour=rgb(0.6,0.6,1), width=7) +
geom_line(aes(y=menle-0.48), colour=rgb(0.6,0.6,1), width=7) +
geom_line(aes(y=menle+0.38), colour=rgb(0.525,0.525,1), width=7) +
geom_line(aes(y=menle-0.38), colour=rgb(0.525,0.525,1), width=7) +
geom_line(aes(y=menle+0.28), colour=rgb(0.45,0.45,1), width=7) +
geom_line(aes(y=menle-0.28), colour=rgb(0.45,0.45,1), width=7) +
geom_line(aes(y=menle+0.18), colour=rgb(0.375,0.375,1), width=7) +
geom_line(aes(y=menle-0.18), colour=rgb(0.375,0.375,1), width=7) +
geom_line(aes(y=menle+0.08), colour=rgb(0.3,0.3,1), width=7) +
geom_line(aes(y=menle-0.08), colour=rgb(0.3,0.3,1), width=7) +
geom_line(aes(y=menle), colour="blue") )
but I can't test it.
Jim
On Thu, May 11, 2017 at 6:05 AM, Kristi Glover
<kristi.glover at hotmail.com> wrote:> Hi R Users,
>
> I was trying to create a figure with geom_ribbon. There is a function
"fill", but I want to make the shaded area with a gradient (increasing
dark color towards a central line, inserted of having a color). Is there any
possibility?
>
>
> In the given example, I want the colour with "blue" but in a
gradient (dark=central, light= as goes higher or lower)
>
>
> pl = data.frame(Time = 0:10, menle = rnorm(11))
>
> pl$menlelb = pl$menle -1
>
> pl$menleub = pl$menle +1
>
> ggplot(pl, aes(Time)) +
>
> geom_line(aes(y=menle), colour="blue") +
>
> geom_ribbon(aes(ymin=menlelb, ymax=menleub), fill="blue")
>
>
> Thanks
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
I haven't tested it but the first thing I'd look at is scale_fill_gradient. HTH Ulrik Jim Lemon <drjimlemon at gmail.com> schrieb am Do., 11. Mai 2017, 07:22:> Hi Kristi, > It can be done, but it is messy: > > pl = data.frame(Time = 0:10, menle = rnorm(11)) > pl$menlelb = pl$menle -1 > pl$menleub = pl$menle +1 > rg<-0.95 > blue<-1 > plot(pl$Time,pl$menlelb,ylim=range(c(pl$menlelb,pl$menleub)),type="l", > lwd=7,col=rgb(rg,rg,blue)) > lines(pl$Time,pl$menlelb,lwd=7,col=rgb(rg,rg,blue)) > rg<-seq(0.9,0.3,length.out=9) > offset<-seq(0.88,0.08,by=-0.1) > for(i in 1:9) { > lines(pl$Time,pl$menle+offset[i],lwd=7,col=rgb(rg[i],rg[i],blue)) > lines(pl$Time,pl$menle-offset[i],lwd=7,col=rgb(rg[i],rg[i],blue)) > } > lines(pl$Time,pl$menle,lwd=6,col=rgb(0,0,blue)) > > For the ggplot solution, this might work: > > ggplot(pl, aes(Time)) + > geom_line(aes(y=menle+1), colour=rgb(0.95,0.95,1), width=7) + > geom_line(aes(y=menle-1), colour=rgb(0.95,0.95,1), width=7) + > geom_line(aes(y=menle+0.88), colour=rgb(0.9,0.9,1), width=7) + > geom_line(aes(y=menle-0.88), colour=rgb(0.9,0.9,1), width=7) + > geom_line(aes(y=menle+0.78), colour=rgb(0.825,0.825,1), width=7) + > geom_line(aes(y=menle-0.78), colour=rgb(0.825,0.825,1), width=7) + > geom_line(aes(y=menle+68), colour=rgb(0.75,0.75,1), width=7) + > geom_line(aes(y=menle-68), colour=rgb(0.75,0.75,1), width=7) + > geom_line(aes(y=menle+0.58), colour=rgb(0.675,0.675,1), width=7) + > geom_line(aes(y=menle-0.58), colour=rgb(0.675,0.675,1), width=7) + > geom_line(aes(y=menle+0.48), colour=rgb(0.6,0.6,1), width=7) + > geom_line(aes(y=menle-0.48), colour=rgb(0.6,0.6,1), width=7) + > geom_line(aes(y=menle+0.38), colour=rgb(0.525,0.525,1), width=7) + > geom_line(aes(y=menle-0.38), colour=rgb(0.525,0.525,1), width=7) + > geom_line(aes(y=menle+0.28), colour=rgb(0.45,0.45,1), width=7) + > geom_line(aes(y=menle-0.28), colour=rgb(0.45,0.45,1), width=7) + > geom_line(aes(y=menle+0.18), colour=rgb(0.375,0.375,1), width=7) + > geom_line(aes(y=menle-0.18), colour=rgb(0.375,0.375,1), width=7) + > geom_line(aes(y=menle+0.08), colour=rgb(0.3,0.3,1), width=7) + > geom_line(aes(y=menle-0.08), colour=rgb(0.3,0.3,1), width=7) + > geom_line(aes(y=menle), colour="blue") ) > > but I can't test it. > > Jim > > On Thu, May 11, 2017 at 6:05 AM, Kristi Glover > <kristi.glover at hotmail.com> wrote: > > Hi R Users, > > > > I was trying to create a figure with geom_ribbon. There is a function > "fill", but I want to make the shaded area with a gradient (increasing dark > color towards a central line, inserted of having a color). Is there any > possibility? > > > > > > In the given example, I want the colour with "blue" but in a gradient > (dark=central, light= as goes higher or lower) > > > > > > pl = data.frame(Time = 0:10, menle = rnorm(11)) > > > > pl$menlelb = pl$menle -1 > > > > pl$menleub = pl$menle +1 > > > > ggplot(pl, aes(Time)) + > > > > geom_line(aes(y=menle), colour="blue") + > > > > geom_ribbon(aes(ymin=menlelb, ymax=menleub), fill="blue") > > > > > > Thanks > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]