[R] order in stacked barplot

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Nov 13 05:53:08 CET 2012


Posting guide warns you to provide a reproducible example, which would include enough data to test code.

As a general point of information I can say that data ordering in R graphs is normally driven by the factor level ordering, and you usually have to specify that explicitly when you convert from character to factor.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

kay <kay.luebke at uni-rostock.de> wrote:

>Hello
>i did a stacked barplot using ggplot and R arranged the bars of the
>items in
>different orders. i don´t know why. but i want to have the same order
>in
>every stacked bar.
> 
>I used the code
>
>data1 <- read.table("N_O_W_MAI.txt", header=TRUE, dec = ",")
>attach(data1)
>Teich1<-factor(Teich,levels=c(5,7,9,11,"G") ,ordered=is.ordered(Teich))
>Gruppe1<-factor(Gruppe,levels=c("Annelida","Cladocera","Copepoda",
>"Diptera","Ephemeroptera","Ostracoda","Sonstige"),ordered=is.ordered(Gruppe))
>   
>data2<-data.frame(Teich1,Gruppe,Herkunft,IRI)
>data2
>> str(data2)
>'data.frame':   65 obs. of  3 variables:
>$ Teich1: Factor w/ 5 levels "2","4","8","10",..: 1 1 1 1 1 1 1 1 1 1
>...
>$ Gruppe1: Factor w/ 7 levels "Annelida","Asellus",..: 1 2 3 4 5 6 7 1
>2 3
>...
> $ IRI   : num  0.606 16.274 0.326 0.396 0 ...
>attach(data2)
>#?data.frame
>str(data2)
> library(ggplot2)
>
>ggplot(data2, aes(Teich1,IRI, fill=Gruppe1)) +
> geom_bar(stat="summary", fun.y="sum") +
>  facet_grid(~Herkunft)
>
>So i get a plot with stacked bars for every "Teich1"(Pond), the stacks
>show
>the summed proportion ("IRI") of the diffrent species ("Gruppe1") in
>every
>pond.  Every species has a discret colour, but the order of species is
>displayed different for the ponds. In the original data they have the
>same
>order for every pond. This is what i want to have for the stacked
>barplot,
>too.
>Has anybody an idea?
>thanks
>Kay  
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/order-in-stacked-barplot-tp4649321.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list