basePlotStackArea.R

Plain text source: basePlotStackArea.R


# -*- Mode:R; Coding:utf-8; fill-column:160 -*-

################################################################################################################################################################
# @file      stackedAreaChart.R
# @author    Mitch Richling <https://www.mitchr.me>
# @Copyright Copyright 2015 by Mitch Richling.  All rights reserved.
# @brief     Draw a filled in area graph with base R.@EOL
# @Keywords  R base area plot graphics
#
# In my opinion, this kind of graph is a almost always a poor way to communicate information -- ranking with a pie chart.  Individual time series (in one
# facet with colors or in multiple facets) along with a single cumulative sum is almost always more clear.  But, you ask for it... ;)
#

################################################################################################################################################################
# First we need some data

x <- 1:30
y <- cbind((30:1)*(1:30)*3,
           (1:30)*(1:30),
           (30:1)*(30:1)
           )

################################################################################################################################################################

par(mar=c(5,5,5,5))

plot(c(x,x), c(rowSums(y), y[,1]),                 # Setup the plot window and ranges
     col=NA,                                       # Do not actually draw anything.
     main='Area Plot', xlab='x', ylab='y')         # Need to do something reasonable for the y label

for(i in (dim(y)[2]):1) {
  yd <- rowSums(matrix(y[,1:i], ncol=i))           # Compute sum
  polygon(c(x, rev(x)),                            # Draw filled area
          c(yd, rep(par('usr')[3], dim(y)[1])),
          col=i,  border=NA)
  points(x, yd)                                    # Draw the points
}

box()                                              # Redraw the box because the polygons may have drawn over the axis box edges
plot of chunk auto-report

The R session information (including the OS info, R version and all packages used):

    options(width=80)
    sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] graphics  grDevices datasets  utils     grid      stats     base     

other attached packages:
[1] RColorBrewer_1.1-2 reshape2_1.4.1     ggplot2_2.1.0      dplyr_0.4.3       
[5] data.table_1.9.6   gridExtra_2.2.1    knitr_1.13         lattice_0.20-33   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      assertthat_0.1   plyr_1.8.3       chron_2.3-47    
 [5] R6_2.1.2         gtable_0.2.0     DBI_0.4-1        formatR_1.4     
 [9] magrittr_1.5     evaluate_0.9     scales_0.4.0     highr_0.6       
[13] stringi_1.0-1    tools_3.3.0      stringr_1.0.0    munsell_0.4.3   
[17] parallel_3.3.0   colorspace_1.2-6 methods_3.3.0   
    Sys.time()
[1] "2016-07-09 20:07:34 CDT"