Greetings: I wish to create a stacked line graph in xyplot, adding color to the spaces between the lines. For example, the code below creates a plot with two lines extending across it, and I want to color the rhomboid that is between the upper and lower line, and between the lower line and the bottom of the frame. Suggestions appreciated --Seth Bigelow library(lattice) x=seq(1,10) #define independent variable lo = 1+1.5*x #create lower line hi = lo + (0.2 + 1.3*x) # create upper line, add it to lower xyplot( lo+hi~x, type="l", main = "Stacked Line plot", xlim=c(1,10))
quote: I wish to create a stacked line graph in xyplot, adding color to the spaces between the lines. For example, the code below creates a plot with two lines extending across it, and I want to color the rhomboid that is between the upper and lower line, and between the lower line and the bottom of the frame. Suggestions appreciated --Seth Bigelow ---------- I think polygon() will do what you want. Carl
On Thu, Sep 18, 2008 at 5:14 PM, Seth W Bigelow <sbigelow at fs.fed.us> wrote:> > Greetings: > I wish to create a stacked line graph in xyplot, adding color to the > spaces between the lines. For example, the code below creates a plot with > two lines extending across it, and I want to color the rhomboid that is > between the upper and lower line, and between the lower line and the bottom > of the frame. > Suggestions appreciatedWell, if you relax the condition to use lattice, try install.packages("ggplot2") library(ggplot2) x <- rep(seq(1,10), 2) y <- c(1+1.5*(1:10), 0.2 + 1.3*(1:10) ) trt <- rep(c("low", "high"), each = 10) qplot(x, y, fill=trt, geom="area") Hadley -- http://had.co.nz/
Ok, thanks to Carl Witthoft I now know that to color spaces between and
below a pair of lines in xyplot, I will need to redefine the lines as
polygons, and use the lpolygon panel function, as in the following
library(lattice)
p <- c(1,10,10,1) # vector of x values
q <- c(4,29.2,16,2.5) # vector of y values for upper polygon
r <- c(2.5,16,0,0) # vector of y values for lower
polygon
xyplot(
q + r ~ p,
panel = lpolygon,
col = c("red","green")
)
But, having spent several hours arriving at this increment of knowlege, I
find myself stumped at how to assign different colors to the two polygons!
the above "col" line did not do the job.
--Seth
Dr. Seth W. Bigelow
Biologist, Sierra Nevada Research Center
Pacific Southwest Research Station, USDA Forest Service. 1731 Research Park
Drive, Davis CA 95618
sbigelow at fs.fed.us
www.fs.fed.us/psw/programs/snrc/staff/bigelow
www.swbigelow.net
Phone: 530 759 1718
Fax: 530 747 0241
Seth W Bigelow wrote:> Ok, thanks to Carl Witthoft I now know that to color spaces between and > below a pair of lines in xyplot, I will need to redefine the lines as > polygons, and use the lpolygon panel function, as in the following > > library(lattice) > p <- c(1,10,10,1) # vector of x values > q <- c(4,29.2,16,2.5) # vector of y values for upper polygon > r <- c(2.5,16,0,0) # vector of y values for lower > polygon > > xyplot( > q + r ~ p, > panel = lpolygon, > col = c("red","green") > ) > > But, having spent several hours arriving at this increment of knowlege, I > find myself stumped at how to assign different colors to the two polygons! > the above "col" line did not do the job. > >Hi Seth, Maybe you could use the stackpoly function in the plotrix package: x<-matrix(x,nrow=10) y<-matrix(y,nrow=10) stackpoly(x,y,stack=TRUE) Jim
Jim Lemon wrote:> Seth W Bigelow wrote: >> Ok, thanks to Carl Witthoft I now know that to color spaces between and >> below a pair of lines in xyplot, I will need to redefine the lines as >> polygons, and use the lpolygon panel function, as in the following >> >> library(lattice) >> p <- c(1,10,10,1) # vector of x values >> q <- c(4,29.2,16,2.5) # vector of y values for upper polygon >> r <- c(2.5,16,0,0) # vector of y values for lower >> polygon >> >> xyplot( >> q + r ~ p, >> panel = lpolygon, >> col = c("red","green") >> ) >> >> But, having spent several hours arriving at this increment of knowlege, I >> find myself stumped at how to assign different colors to the two >> polygons! >> the above "col" line did not do the job. >> >> > Hi Seth, > Maybe you could use the stackpoly function in the plotrix package: > > x<-matrix(x,nrow=10) > y<-matrix(y,nrow=10) > stackpoly(x,y,stack=TRUE) > > JimAlso see the filled bands option in the xYplot function in the Hmisc package. Frank -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
R-listers:
I am summarizing input I got on a recent query about creating stacked,
colored line plots in xyplot.
The only input I got regarding xyplot was to use the polygon() command, but
this seemed to require some awkward data manipulation.
In contrast, I got several snippets of functional code for use in the
ggplot2 and plotrix packages, both of which produced nice graphs with
minimal effort. (Apparently the filled bands option in the xYplot function
in the Hmisc package will also do this).
### stacked line graphs in plotrix package
x <-matrix(x,nrow=10) # create matrix of x values
y <-matrix(y,nrow=10) # create matrix of y values
stackpoly(x,y,stack=TRUE) # make the graph
### stacked line graphs in ggplot2 package
x <- rep(seq(1,10), 2) # create column of x values
y <- c(1+1.5*(1:10), 0.2 + 1.3*(1:10) ) # create column of y values
trt <- rep(c("low", "high"), each = 10) # create column
with identify of
treatments
qplot(x, y, fill=trt, geom="area") # plot the graph
I ended up using ggplot2, because I had to churn out a large number of
these, and the "facets" command allowed me to make panels
in a manner akin to the lattice system.
Thanks to Carl, Hadley, Xie, Jim and Frank for responding.
--Seth Bigelow
Dr. Seth W. Bigelow
Biologist, Sierra Nevada Research Center
Pacific Southwest Research Station, USDA Forest Service. 1731 Research Park
Drive, Davis CA 95618