Is there any way in R to do a stacked area plot where the outcome is not proportions but absolute values, i.e. the slices should not add up to 1, but should represent the actual values of each factor?
Asked
Active
Viewed 1,350 times
0
-
3Some code would be nice to show us what you tried, and the answer on this question will give you the solution to your problem : http://stackoverflow.com/questions/4651428/making-a-stacked-area-plot-using-ggplot2 Using the search function can solve many problems... – Joris Meys May 26 '11 at 08:29
3 Answers
2
An example using ggplot:
library(ggplot2)
library(reshape2)
dat <- data.frame(
x = LETTERS[1:3],
series1 = 1:3,
series2 = 4:6)
ggplot(melt(dat), aes(x=x, y=value, fill=variable, group=variable)) + geom_area()

Andrie
- 176,377
- 47
- 447
- 496
0
- Use
geom_area(position="fill")
if you want to plot the proportions. - Use
geom_area(position="stack")
if you want to plot the absolute values.geom_area()
will also default to this.

double-beep
- 5,031
- 17
- 33
- 41

Tom Davidson
- 737
- 1
- 8
- 16
0
With base graphics you can use apply and cumsum to get the heights of the points, if you just want the lines without the area filled in you could then use the matplot function. If you want the areas filled in then you can create the initial plot with type='n' (or using just the last set of coords), the use a loop (either explicit for loop, or apply) along with the polygon function to add the polygons. You can either start with the top points and plot the polygon from 0 to the points and have later polygons cover the bottoms of earlier ones, or you can do the polygons between adjacent sets of points.
Once you have done this once you can wrap the code into a function and make future plots that much quicker.

Greg Snow
- 48,497
- 6
- 83
- 110