# Creating a color shade between upper and lower bounds of a line plot in R Classic List Threaded 5 messages Reply | Threaded
Open this post in threaded view
|

## 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.pngI 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 [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

## Re: 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 <[hidden email]> On Behalf Of rain1290--- via R-sig-Geo Sent: Friday, April 5, 2019 10:22 AM To: [hidden email] 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.pngI 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 [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo_______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

## Re: 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 <[hidden email]> To: [hidden email] <[hidden email]>; [hidden email] <[hidden email]> 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 <[hidden email]> On Behalf Of rain1290--- via R-sig-Geo Sent: Friday, April 5, 2019 10:22 AM To: [hidden email] 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.pngI 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 [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo        [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

## Re: Creating a color shade between upper and lower bounds of a line plot in R

 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") #   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, x, 25.96, 25.96, rev(x), edge) ,      c(sqrt(edge), y1, sqrt(edge), edge/6, rev(y2), edge/6),      col=greena) ---------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77843-4352 From: [hidden email] <[hidden email]> Sent: Friday, April 5, 2019 11:26 AM To: David L Carlson <[hidden email]>; [hidden email] 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 To: mailto:[hidden email] ; mailto:[hidden email] 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 On Behalf Of rain1290--- via R-sig-Geo Sent: Friday, April 5, 2019 10:22 AM To: mailto:[hidden email] 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.pngI 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:[hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo_______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

## Re: Creating a color shade between upper and lower bounds of a line plot in R

 In reply to this post by R-sig-geo mailing list Ah, I see. But let's say I had 10 lines on the same plot and wanted to shade green between them. Would this still work, or it only works for 2 lines? Also, for the "rev" function in there, I noticed that it does not actually "reverse" anything, but is it necessary? Thanks, again.  -----Original Message----- From: David L Carlson <[hidden email]> To: [hidden email] <[hidden email]>; [hidden email] <[hidden email]> Sent: Fri, Apr 5, 2019 12:59 pm Subject: RE: [R-sig-Geo] Creating a color shade between upper and lower bounds of a line plot in R 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") #   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, x, 25.96, 25.96, rev(x), edge) ,     c(sqrt(edge), y1, sqrt(edge), edge/6, rev(y2), edge/6),     col=greena) ---------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77843-4352 From: [hidden email] <[hidden email]> Sent: Friday, April 5, 2019 11:26 AM To: David L Carlson <[hidden email]>; [hidden email] 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 To: mailto:[hidden email] ; mailto:[hidden email] 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 On Behalf Of rain1290--- via R-sig-Geo Sent: Friday, April 5, 2019 10:22 AM To: mailto:[hidden email] 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.pngI 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:[hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo        [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo