0

I want to plot this style -graph here scaled with proportions. Is there some ready plot for that in R?

Suppose I have nominal values per year in a matrix. With matplot(..), I can plot easily all nominal values. If I divide the values by the aggregate sum, I do not get them scaled but jammed up so I would have to adjust things in that case. Better, is there something such as a package that could do that kind of allocation analysis directly perhaps in a report -form?

I want an area plot for the following data where XN is time X1 is step 1, X2 is step 2 --. Now there are about 60 vars. How can I create an proportional area plot for that?

> head(b)
        X0       X1       X2       X3       X4       X5       X6
1  2803.30  2698.05  2428.24  2000.00  3917.54  4575.02  2656.00
2   227.46   220.68   200.00    30.00   220.43    30.00    30.00
3   855.33   824.54   700.00    30.00   242.52  1190.70  1906.01
4  2777.37  2764.57  1265.66  3326.28  4094.00  7049.84 10726.76
5    60.50    59.60     1.00    30.00    30.00    30.00    82.67
6 14548.16 14548.16 14664.79 19855.44 24674.03 23414.45 14973.54

> dput(head(b))

structure(list(X0 = c(2803.3, 227.46, 855.33, 2777.37, 60.5, 
14548.16), X1 = c(2698.05, 220.68, 824.54, 2764.57, 59.6, 14548.16
), X2 = c(2428.24, 200, 700, 1265.66, 1, 14664.79), X3 = c(2000, 
30, 30, 3326.28, 30, 19855.44), X4 = c(3917.54, 220.43, 242.52, 
4094, 30, 24674.03), X5 = c(4575.02, 30, 1190.7, 7049.84, 30, 
23414.45), X6 = c(2656, 30, 1906.01, 10726.76, 82.67, 14973.54
)), .Names = c("X0", "X1", "X2", "X3", "X4", "X5", "X6"), row.names = c(NA, 
6L), class = "data.frame")

Related questions

  1. Making a stacked area plot using ggplot2
  2. Getting a stacked area plot in R
Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282
  • 2
    Why are financial charts so garish? Minimalism (Apple excepted, I guess) and the work of Tufte seems to have been ignored by the financial sector. That web page is full of macho-graphics. If there were more women in finance.... – Spacedman Feb 18 '12 at 14:35

1 Answers1

1

If you had a matrix you could first apply cumsum to the columns and divide the result by sum applied to the columns. The final result could be given back to matplot. I suspect you could do the entire transformation in one step with sweep, but I'm not particularly facile with that function.

This would be a matplot solution (I originally had this by columns but your data was arranged differently than I expected):

matplot(t(apply(b, 1, function(x) cumsum(x)/sum(x) )),type="l")

If you go to the R Graphics library you can often find plots that do your particular desires. In this case it looks as though the conditional density plot by Zeileis in package vcd may be a good fit:

http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=120

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • `colSums(b[1:7,]/colSums(b[1:7,]))` ...why does this not return `1 1 1 1 ...`? Irritating this `sum(b[1:7,][1]/22772.12)` does as expected. There is something wrong in dividing by colSums... – hhh Feb 18 '12 at 18:23
  • I said to use `cumsum` with `sum`. Otherwise I don't see how you would get the proportions to work out correctly. – IRTFM Feb 18 '12 at 22:03