[R-sig-Geo] Creating a color shade between upper and lower bounds of a line plot in R
David L Carlson
dc@r|@on @end|ng |rom t@mu@edu
Fri Apr 5 18:59:02 CEST 2019
The two lines can have different x coordinates. We are defining the boundaries of the polygon by tracing clockwise or counterclockwise around it, e.g. In this example we follow the top line from beginning to end and then follow the bottom line from end back to beginning. The function automatically connects the beginning and end points to define the polygon and then shades it. The x values do not need to be the same for the two lines, e.g.
lines(x1, y1)
lines(x2, y2)
polygon(c(x1, rev(x2)), c(y1, rev(y1)))
Things are a bit trickier if you want the polygon to start and end at the edge of the plot window. You can get the coordinates of the plot window boundaries with the following:
par("usr")
# [1] 0.04 25.96 -0.20 5.20
This gives the left lower corner (.04, -.02) and the upper right corner (25.96, 5.20). We only need the x values: .04 on the left and 25.96 on the right. We insert x-coordinates into the polygon, but we have to compute the y-axis values at .04 and 25.96 using the formulae you are using to define those lines.
x <- 1:25
y1 <- sqrt(x)
y2 <- x/6
y3 <- y2 + .5
plot(x, y1, typ="l", ylim=c(0, 5))
lines(x, y2)
lines(x, y3)
greena <- rgb(0, 255/255, 0, .5)
edge <- par("usr")
polygon(c(edge[1], x, 25.96, 25.96, rev(x), edge[1]) ,
c(sqrt(edge[1]), y1, sqrt(edge[2]), edge[2]/6, rev(y2), edge[1]/6),
col=greena)
----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352
From: rain1290 using aim.com <rain1290 using aim.com>
Sent: Friday, April 5, 2019 11:26 AM
To: David L Carlson <dcarlson using tamu.edu>; r-sig-geo using r-project.org
Subject: Re: [R-sig-Geo] Creating a color shade between upper and lower bounds of a line plot in R
Hi David,
Okay, I just gave this a try and was able to generate a shaded area using my existing code. But just to make sure that I am using this correctly, I did this using the following
plot(get, Hope2, type = "l",col = "green", lwd = "3", xlab="Cumulative CO2 emissions (TtC)", ylab = "One-day maximum precipitation (mm/day)", main = "One-day maximum precipitation for Random Area for CanESM2 scenarios")lines(IPSL, Hope6, type = "l", lwd = "3", col = "green")
lines(MIROC, Hope7, type = "l", lwd = "3", col = "green")
lines(subsetprime, Hope8, type = "l", lwd = "3", col = "green")
lines(MPI, Hope9, type =" l", lwd = "3", col = "green")
polygon(c(get,rev(get)), c(Hope2,rev(Hope9)),col="green") #this is what I just tried
The polygon line that I just added there - am I telling R to shade everything from Hope2 to Hope9?
In this case, I have different x and y values for each "lines". Do I need to specify the polygon function each time I use the "lines" function in order to capture "all" of the green curves?
Thanks, again,
-----Original Message-----
From: David L Carlson <mailto:dcarlson using tamu.edu>
To: mailto:rain1290 using aim.com <mailto:rain1290 using aim.com>; mailto:r-sig-geo using r-project.org <mailto:r-sig-geo using r-project.org>
Sent: Fri, Apr 5, 2019 11:53 am
Subject: RE: [R-sig-Geo] Creating a color shade between upper and lower bounds of a line plot in R
You can use the polygon() function (?polygon). Here's a simple reproducible example:
x <- 1:25
y1 <- sqrt(x)
y2 <- x/6
plot(x, y1, typ="l", ylim=c(0, 5))
lines(x, y2)
polygon(c(x, rev(x)) , c(y1, rev(y2)), col="green")
lines(x, y2+.5)
If there are lines hidden by the shading, plot them after the polygon or add an alpha value to the fill color you are using, e.g.
greena <- rgb(0, 255/255, 0, .5)
polygon(c(x, rev(x)) , c(y1, rev(y2)), col=greena)
----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352
-----Original Message-----
From: R-sig-Geo <mailto:r-sig-geo-bounces using r-project.org> On Behalf Of rain1290--- via R-sig-Geo
Sent: Friday, April 5, 2019 10:22 AM
To: mailto:r-sig-geo using r-project.org
Subject: [R-sig-Geo] Creating a color shade between upper and lower bounds of a line plot in R
Hello there,
I have many line plots on a single graph and would like to somehow create a shaded area between them so that it looks neater and easier to interpret. Something like this:
http://jvoigts.scripts.mit.edu/blog/assets/plot_shaded_pretty.png
I heard that the function "geom_ribbon" was ideal to do this, but is it possible to incorporate this in the basic "plot" command? If so, how?
This is currently what I have done with the basic plotting, which works fine:
plot(get, Hope2, type = "l",col = "green", lwd = "3", xlab="Cumulative CO2 emissions (TtC)", ylab = "One-day maximum precipitation (mm/day)", main = "One-day maximum precipitation for Random Area for CanESM2 scenarios")lines(IPSL, Hope6, type = "l", lwd = "3", col = "green")
lines(MIROC, Hope7, type = "l", lwd = "3", col = "green")
lines(subsetprime, Hope8, type = "l", lwd = "3", col = "green")
lines(MPI, Hope9, type =" l", lwd = "3", col = "green")
So, in this plot, I have 5 green line plots in one. The idea would be shade these in green, from the lower to upper bounds. Is this possible?
Any assistance would be extremely valuable!
[[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
mailto:R-sig-Geo using r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list