Hi all, I have a large data frame and would like to make a barplot of a categorical variable with the bars sorted in order of decreasing frequency. # Example: v1 = c(1.2, 1.4, 0.9, 1.0, 1.1, 1.0) v2 = c("aa", "cc", "bb", "bb", "cc", "bb") v3 = c(8, 10, 11, 9, 9, 10) df = data.frame(v1=v1, v2=v2, v3=v3) # How can I tell ggplot to sort the bars? # First bar = "bb" (3), second bar "cc" (2) and third bar "aa" (1) p = gplot(df) p + aes(v2) + geom_bar() Thank you, Morten -- View this message in context: http://r.789695.n4.nabble.com/ggplot2-barplot-in-decreasing-frequency-tp2964511p2964511.html Sent from the R help mailing list archive at Nabble.com.
Hi Morten Just order the factor the way you want before plotting: df$v2 <- factor(df$v2, levels=c("bb", "cc", "aa")) p = ggplot(df) p + aes(v2) + geom_bar() Best, Ista On Wed, Oct 6, 2010 at 5:09 AM, Morten <Morten.Lindberg at siv.no> wrote:> > Hi all, > > I have a large data frame and would like to make a barplot of a categorical > variable with the bars sorted in order of decreasing frequency. > > # Example: > v1 = c(1.2, 1.4, 0.9, 1.0, 1.1, 1.0) > v2 = c("aa", "cc", "bb", "bb", "cc", "bb") > v3 = c(8, 10, 11, 9, 9, 10) > df = data.frame(v1=v1, v2=v2, v3=v3) > > # How can I tell ggplot to sort the bars? > # First bar = "bb" (3), second bar "cc" (2) and third bar "aa" (1) > > p = gplot(df) > p + aes(v2) + geom_bar() > > > Thank you, > > Morten > -- > View this message in context: http://r.789695.n4.nabble.com/ggplot2-barplot-in-decreasing-frequency-tp2964511p2964511.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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Hi: Another approach is: v1 = c(1.2, 1.4, 0.9, 1.0, 1.1, 1.0) v2 = c("aa", "cc", "bb", "bb", "cc", "bb") v3 = c(8, 10, 11, 9, 9, 10) df = data.frame(v1=v1, v2=v2, v3=v3) ggplot(df) + geom_bar(aes(x = reorder(v2, desc(v3)))) HTH, Dennis On Wed, Oct 6, 2010 at 2:09 AM, Morten <Morten.Lindberg@siv.no> wrote:> > Hi all, > > I have a large data frame and would like to make a barplot of a categorical > variable with the bars sorted in order of decreasing frequency. > > # Example: > v1 = c(1.2, 1.4, 0.9, 1.0, 1.1, 1.0) > v2 = c("aa", "cc", "bb", "bb", "cc", "bb") > v3 = c(8, 10, 11, 9, 9, 10) > df = data.frame(v1=v1, v2=v2, v3=v3) > > # How can I tell ggplot to sort the bars? > # First bar = "bb" (3), second bar "cc" (2) and third bar "aa" (1) > > p = gplot(df) > p + aes(v2) + geom_bar() > > > Thank you, > > Morten > -- > View this message in context: > http://r.789695.n4.nabble.com/ggplot2-barplot-in-decreasing-frequency-tp2964511p2964511.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]