Stacked geom_area

407 views
Skip to first unread message

donnek

unread,
Dec 10, 2010, 8:17:21 AM12/10/10
to ggplot2
Hi

I am relatively new to R and ggplot2m, and I am trying to do a stacked
geom_area for data in the following format:

str(allprofile)
'data.frame': 932 obs. of 4 variables:
$ id : int 1 2 3 4 5 6 7 8 9 10 ...
$ my2: int 0 4 2 1 4 1 0 0 0 0 ...
$ my0: int 0 0 0 0 0 0 0 0 0 0 ...
$ my3: int 13 3 1 3 0 4 14 3 6 9 ...

Basically, for each id I want a slice showing my3 at the bottom, with
my0 (if any) and my2 stacked on top of it.

From reading over some of the other posts about geom_area, I get the
feeling the data is in the wrong format, and needs to be something
like this:
id, variable, value
1, my3, 13
2, my2, 4
2, my3, 3
3, my2, 2
3, my3, 1
4, my2, 1
4, my3, 3

Is this what needs to be done, and if so, is there a simple way of
reformatting? If the data is OK as is, what invocation should I use
for a stacked geom_area?

Thanks.

Kevin

Kohske Takahashi

unread,
Dec 10, 2010, 11:26:59 AM12/10/10
to donnek, ggplot2
Hi, here is an example:

# raw data
n <- 5
d <- data.frame(id=1:n, m0=sample(n), m1=sample(n), m2=sample(n))

# reshape by melt
d2 <- melt(d, id="id")

# reorder the factor
d2$variable <- factor(d2$variable, levels=c("m2","m0","m1"))

# plot
ggplot(d2, aes(id, value, fill=variable)) + geom_area()

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Kevin Donnelly

unread,
Dec 10, 2010, 3:36:05 PM12/10/10
to ggplot2
Hi

Thanks for this. After importing the data as "allprofile", I used:
d2 <- melt(allprofile, id="id")
d2$variable <- factor(d2$variable, levels=c("my2","my0","my3"))


ggplot(d2, aes(id, value, fill=variable)) + geom_area()

However, it looks as if the legibility of the plot breaks down after about 50
observations, so 932 is probably too many. Converting the figures to
percentages is better in some ways.

In both cases, is the plot in order of the id? Some parts seemed as if they
were not in id sequence.

Thanks.

Kevin

::::On Friday 10 December 2010 Kohske Takahashi said::::

--
Pob hwyl / Best wishes

Kevin Donnelly
kevindonnelly.org.uk

Reply all
Reply to author
Forward
0 new messages