elpape wrote on 12/30/2011 09:04:53 AM:

> Dear all,
> 
> I would like to create a vertically stacked area chart in R. The data 
are
> presented in the attached text file. 
> I would like to see the trend in values for the different groups with
> sediment depth (that's why I would like to create a vertically stacked
> chart; normally sed_depth should be = x, but I want it plotted on the
> y-axis). In the packages available to create stacked area plots, there 
seems
> to be an option for horizontally stacked area plots only. Does anybody 
know
> how I should adjust the code for creating the graph that I want?
> 
> http://r.789695.n4.nabble.com/file/n4245931/FT_GB0806.txt FT_GB0806.txt 
> 
> I hope this message is clear! Thanks for the help!
> 
> Ellen


I'm not sure what's available in packages for area plots.  You may wish to 
write your own code using the polygon() function.  Here's an example.

df <- structure(list(sed_depth = c("0-1", "1-2", "2-3", "3-4", "4-10", 
"0-1", "1-2", "2-3", "3-4", "4-10", "0-1", "1-2", "2-3", "3-4", 
"4-10", "0-1", "1-2", "2-3", "3-4", "4-10"), FT = c("1A", "1A", 
"1A", "1A", "1A", "1B", "1B", "1B", "1B", "1B", "2A", "2A", "2A", 
"2A", "2A", "2B", "2B", "2B", "2B", "2B"), value = c(35.165917, 
36.214478, 19.097014, 35.869533, 30.048236, 15.02829, 19.156373, 
12.454054, 25.231304, 22.94187, 47.584233, 38.368717, 44.382409, 
31.247407, 29.81555, 2.221559, 6.260432, 24.066523, 7.651756, 
17.194344)), .Names = c("sed_depth", "FT", "value"), row.names = c(NA, 
-20L), class = "data.frame")

# matrix of values by y and area categories
m <- tapply(df$value, list(df$sed_depth, df$FT), mean)

# y-axis labels and midpoints
sed.depth.lab <- dimnames(m)[[1]]
ny <- length(sed.depth.lab)
sed.depth.mid <- -sapply(strsplit(sed.depth.lab, "-"), 
        function(x) mean(as.numeric(x)))

# cumulative values
cum.value <- cbind(rep(0, ny), t(apply(m, 1, cumsum)))

# area labels and midpoints
FT.lab <- dimnames(m)[[2]]
na <- length(FT.lab)
FT.mid <- (cum.value[1, 1:na] + cum.value[1, 1+(1:na)]) / 2

# stacked area plot
plot(0, 0, xlim=c(0, max(cum.value)), ylim=range(sed.depth.mid), 
        type="n", axes=FALSE, xlab="Value", ylab="Sed Depth")
for(i in 1+(1:na)) {
        polygon(c(cum.value[, i-1], rev(cum.value[, i])), 
                c(sed.depth.mid, rev(sed.depth.mid)), 
                density=NA, col=gray(i/(na+2)))
        }
axis(1)
axis(2, at=sed.depth.mid, labels=sed.depth.lab, las=1)
axis(3, at=FT.mid, labels=FT.lab)
box()


By the way, it's useful to readers on the list if you share your data 
using readily submittable code, like the output from the function dput(), 
rather than as an attachment.

Jean

	[[alternative HTML version deleted]]

