Skip to content

Stacked Area chart

3 messages · Mike Saunders, Achim Zeileis, Christian Lasarczyk

#
On Tue, 16 Aug 2005 12:03:01 -0400 Mike Saunders wrote:

            
If you don't want to show the marginal distribution over time, then you
can use barplot() on the table of relative frequencies (e.g., obtained
by prop.table()).
If you want to show the marginal distribution, you can use mosaicplot().
The functions doubledecker() and mosaic() in package vcd might also be
helpful.
Z
#
Maybe this is useful:


stackedPlot <- function(data, time=NULL, col=1:length(data), ...) {

  if (is.null(time))
    time <- 1:length(data[[1]]);
  
  plot(0,0
       , xlim = range(time)
       , ylim = c(0,max(rowSums(data)))
       , t="n" 
       , ...
       );
  
  for (i in length(data):1) {

    # Die Summe bis zu aktuellen Spalte
    prep.data <- rowSums(data[1:i]);
    
    # Das Polygon muss seinen ersten und letzten Punkt auf der Nulllinie haben
    prep.y <- c(0
                , prep.data
                , 0
                )

    prep.x <- c(time[1]
                , time
                , time[length(time)]
                )
    
    polygon(prep.x, prep.y
            , col=col[i]
            , border = NA
            );
  }
}

dogs <- runif(10)+ 1:10;
cats <- 11 - dogs;
birds <- 11 - cats;
population <- data.frame(dogs,cats,birds);
stackedPlot(population);

Documentation is bad (as this function is for personal use) and you
may want to normalize your data, but it should be useful for different
sized data.frames.

Best regards,
     Christian 


2005/8/16, Mike Saunders <mike_saunders at umenfa.maine.edu>: