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 -- View this message in context: http://r.789695.n4.nabble.com/vertically-stacked-area-plot-tp4245931p4245931.html Sent from the R help mailing list archive at Nabble.com.
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 dataare> 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, thereseems> to be an option for horizontally stacked area plots only. Does anybodyknow> 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! > > EllenI'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]]
Ellen, Is this what you are looking for? library(HH) sed <- read.table(textConnection(" sed_depth FT value 0-1 1A 35.16591742 1-2 1A 36.21447839 2-3 1A 19.09701388 3-4 1A 35.86953345 4-10 1A 30.04823571 0-1 1B 15.02829003 1-2 1B 19.15637318 2-3 1B 12.45405429 3-4 1B 25.23130364 4-10 1B 22.94187026 0-1 2A 47.58423315 1-2 2A 38.36871661 2-3 2A 44.3824086 3-4 2A 31.24740697 4-10 2A 29.81555036 0-1 2B 2.221559394 1-2 2B 6.260431823 2-3 2B 24.06652322 3-4 2B 7.651755935 4-10 2B 17.19434367 "), header=TRUE) closeAllConnections() sed2 <- matrix(sed$value, 4, 5, dimnames=list(sed$FT[c(1,6,11,16)], sed$sed_depth[1:5]), byrow=TRUE) sed3 <- cbind(sed2[4:1, 5:1], '1'=0, '2'=0, '3'=0, '4'=0, '5'=0) result <- plot.likert(sed3, horizontal=FALSE) result$legend$bottom$args$text <- result$legend$bottom$args$text[1:5] result$legend$bottom$args$rect$col <- result$legend$bottom$args$rect$col[1:5] result$legend$bottom$args$columns=5 result If yes, please write me off-list and we can tinker with the appearance. Rich On Fri, Dec 30, 2011 at 10:04 AM, elpape <ellen.pape@gmail.com> wrote:> 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 > > -- > View this message in context: > http://r.789695.n4.nabble.com/vertically-stacked-area-plot-tp4245931p4245931.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
> 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 >Hi Ellen, Do you want the stackpoly function (plotrix)? Jim