Hi - I'd like to have the area between 2 lines on a x-y plot be filled with grey, but I haven't had any luck using polygon or rect. (In reality, I'd like to do this for twice - once for a low group and once for a high group - and then I'd like to plot a set of data points for a 'normal' group together with these 2 grey areas.) Here's a simple example of the 2 lines: age=1:10 y.low=rnorm(length(age),150,25)+10*age y.high=rnorm(length(age),250,25)+10*age plot(age,y.high,type='n',ylim=c(100,400),ylab='Y Range',xlab='Age (years)') lines(age,y.low,col='grey') lines(age,y.high,col='grey') Is it possible to fill the area between the 2 lines filled with, for example, 'grey30' ? thanks very much in advance, David Freedman ----- David Freedman Atlanta -- View this message in context: http://www.nabble.com/fill-in-area-between-2-lines-with-a-color-tp18556096p18556096.html Sent from the R help mailing list archive at Nabble.com.
try the following: age <- 1:10 y.low <- rnorm(length(age), 150, 25) + 10*age y.high <- rnorm(length(age), 250, 25) + 10*age plot(age,y.high,type = 'n', ylim = c(100, 400), ylab = 'Y Range', xlab = 'Age (years)') lines(age, y.low, col = 'grey') lines(age, y.high, col = 'grey') polygon(c(age, rev(age)), c(y.high, rev(y.low)), col = "grey30", border = NA) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://perswww.kuleuven.be/dimitris_rizopoulos/ Quoting David Freedman <3.14david at gmail.com>:> > Hi - I'd like to have the area between 2 lines on a x-y plot be filled with > grey, but I haven't had > any luck using polygon or rect. (In reality, I'd like to do this for twice > - once for a low group and once for a high group - and then I'd like to plot > a set of data points for a 'normal' group together with these 2 grey areas.) > > Here's a simple example of the 2 lines: > > age=1:10 > y.low=rnorm(length(age),150,25)+10*age > y.high=rnorm(length(age),250,25)+10*age > plot(age,y.high,type='n',ylim=c(100,400),ylab='Y Range',xlab='Age (years)') > lines(age,y.low,col='grey') > lines(age,y.high,col='grey') > > Is it possible to fill the area between the 2 lines filled with, for > example, 'grey30' ? > > thanks very much in advance, > David Freedman > > ----- > David Freedman > Atlanta > -- > View this message in context: > http://www.nabble.com/fill-in-area-between-2-lines-with-a-color-tp18556096p18556096.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
on 07/20/2008 11:34 AM David Freedman wrote:> Hi - I'd like to have the area between 2 lines on a x-y plot be filled with > grey, but I haven't had > any luck using polygon or rect. (In reality, I'd like to do this for twice > - once for a low group and once for a high group - and then I'd like to plot > a set of data points for a 'normal' group together with these 2 grey areas.) > > Here's a simple example of the 2 lines: > > age=1:10 > y.low=rnorm(length(age),150,25)+10*age > y.high=rnorm(length(age),250,25)+10*age > plot(age,y.high,type='n',ylim=c(100,400),ylab='Y Range',xlab='Age (years)') > lines(age,y.low,col='grey') > lines(age,y.high,col='grey') > > Is it possible to fill the area between the 2 lines filled with, for > example, 'grey30' ? > > thanks very much in advance, > David FreedmanTry this: age=1:10 y.low=rnorm(length(age),150,25)+10*age y.high=rnorm(length(age),250,25)+10*age plot(age,y.high,type='n',ylim=c(100,400),ylab='Y Range', xlab='Age (years)') lines(age,y.low,col='grey') lines(age,y.high,col='grey') polygon(c(age, rev(age), age[1]), c(y.low, rev(y.high), y.low[1]), col = "grey30") What you essentially need to do is to 'walk' the boundary of the polygon, be sure to 'close' the open end by returning to the starting point, and then fill it. HTH, Marc Schwartz