ordering with respect to stacking

21 views
Skip to first unread message

adam.l...@pnc.com

unread,
Sep 11, 2012, 3:07:12 PM9/11/12
to ggp...@googlegroups.com
Hi all,

Had a question about stacking if anyone has time.

Not sure if it's unique to factors with three levels, but was having difficulty arranging stacks in a bar plot in the order required.

The following example shows that changing the orders of the factor impacts the color pattern, but not the position of the levels in the stack. On the other hand, using the diamonds dataset, I was able to make the desired adjustment, so it's unclear to me what's unique about my example.

Here's the example showing how changing the order of factors changes the colours assigned to each level, but doesn't impact the position of levels in the stack (as desired and which is achievable with the diamonds dataset).  

        tst<-data.frame(y=c(1,1,1,2,2,2,3,3,3),x=c("a","a","a","b","b","b","c","c","c"),z=rep(c("x","y","z"),3))

        tst$z2<-factor(tst$z,levels=c("z","y","x"))

        ggplot(tst,aes(x,y))+geom_bar(stat="identity",aes(fill=z2),position="stack")
        ggplot(tst,aes(x,y))+geom_bar(stat="identity",aes(fill=z),position="stack")

Thanks very much for the help! Once again.

Adam Loveland

The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.


Ben Bond-Lamberty

unread,
Sep 12, 2012, 9:31:27 PM9/12/12
to adam.l...@pnc.com, ggp...@googlegroups.com
It's the stat="identity" that's screwing things up for you. Try

ggplot(tst,aes(x,y))+geom_bar(aes(fill=z2),position="stack")

which correctly puts z at the bottom of the stacks.

Ben
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
Reply all
Reply to author
Forward
0 new messages