R: Stacked area plot with absolute values
Is there any way in R to do a stacked area plot where the outcome is not proportions but absolute va开发者_Go百科lues, i.e. the slices should not add up to 1, but should represent the actual values of each factor?
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()
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.
- 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.
精彩评论