[R] Ordering of stack in ggplot (package ggplot2)

Dennis Murphy djmuser at gmail.com
Wed Oct 19 04:09:06 CEST 2011


Hi:

levels(df.m2$Region)
[1] "Africa"   "Americas" "Asia"     "Europe"   "Oceania"

Reorder your Region factor to the following:

df.m2$Region <- factor(df.m2$Region, levels = c('Europe', 'Asia',
                            'Americas', 'Africa', 'Oceania'))

Then recopy the code from the definition of a onward and you should
get what you want. Worked for me.

Dennis


On Tue, Oct 18, 2011 at 4:59 PM, swonder03 <ramey.steven at gmail.com> wrote:
> I'm trying to reproduce the 3rd graph on the page of this site:
> http://learnr.wordpress.com/2009/03/17/ggplot2-barplots/ . However, the data
> below produces a ggplot with the stacks sorted in alphabetical order from
> the bottom up. I'd like the stacks to be in the order "Europe", "Asia",
> "Americas, "Africa", "Oceania". Is there an easy way to manipulate ggplot or
> geom_bar to do this?
>
> /library(ggplot2)
> df <- structure(c(106487, 495681, 1597442,
>                                2452577, 2065141, 2271925, 4735484, 3555352,
>                                8056040, 4321887, 2463194, 347566, 621147,
>                                1325727, 1123492, 800368, 761550, 1359737,
>                                1073726, 36, 53, 141, 41538, 64759, 124160,
>                                69942, 74862, 323543, 247236, 112059, 16595,
>                                37028, 153249, 427642, 1588178, 2738157,
>                                2795672, 2265696, 11951, 33424, 62469,
>                                74720, 166607, 404044, 426967, 38972, 361888,
>                                1143671, 1516716, 160037, 354804, 996944,
>                                1716374, 1982735, 3615225, 4486806, 3037122,
>                                17, 54, 55, 210, 312, 358, 857, 350, 7368,
>                                8443, 6286, 1750, 7367, 14092, 28954, 80779,
>                                176893, 354939, 446792, 33333, 69911, 53144,
>                                29169, 18005, 11704, 13363, 18028, 46547,
>                                14574, 8954, 2483, 14693, 25467, 25215,
>                                41254, 46237, 98263, 185986), .Dim = c(19,
>                                5), .Dimnames = list(c("1820-30", "1831-40",
>                                                "1841-50", "1851-60", "1861-70", "1871-80",
>                                                "1881-90", "1891-00", "1901-10", "1911-20",
>                                                "1921-30", "1931-40", "1941-50", "1951-60",
>                                                "1961-70", "1971-80", "1981-90", "1991-00",
>                                                "2001-06"), c("Europe", "Asia", "Americas",
>                                                "Africa", "Oceania")))
>
> df.m2 <- melt(df)
> df.m2 <- rename(df.m2, c(X1 = "Period", X2 = "Region"))
>
> a <- ggplot(df.m2, aes(x = Period, y = value/1e+06,
>                                                fill = Region)) + opts(title = "Migration to the United States by
> Source Region (1820-2006)") +
>                labs(x = NULL, y = "Number of People (in millions)n",
>                                fill = "")
> b <- a + geom_bar(stat = "identity", position = "stack")
> b <- b + scale_fill_brewer(palette = "Set1")
>
> immigration_theme <- theme_update(axis.text.x = theme_text(angle = 90,
>                                hjust = 1), panel.grid.major = theme_line(colour = "grey90"),
>                panel.grid.minor = theme_blank(), panel.background = theme_blank(),
>                axis.ticks = theme_blank(), legend.position = "right")
>
> b/
>
> Thanks in advance
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Ordering-of-stack-in-ggplot-package-ggplot2-tp3917159p3917159.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