[R] ggplot2 Pareto plot (Barplot in decreasing frequency)

David Winsemius dwinsemius at comcast.net
Thu Oct 7 00:03:20 CEST 2010


On Oct 6, 2010, at 5:19 PM, Morten wrote:

>
> Hi all
>
> I have a large dataframe with (among others) a categorical variable  
> of 52
> levels and would like to create a barplot with the bars ordered in
> decreasing frequency of the levels. I belive it is referred to as a  
> pareto
> plot.
>
> Consider a subset where I keep only the categorical variable in  
> question.
>
> # Example:
>
> df = data.frame(v1=v1) v1 =  c("aa", "cc", "bb", "bb", "cc", "bb")
>
> # How can I tell ggplot to sort the bars?

I seem to remember that sorting the levels is the way this has been  
answered in the past for ggplot and ggplot2. So table() "v1" and then  
reverse:

 > v2 <- factor(v1, level=names(table(v1))[rev(order(table(v1)))] )
 > v2
[1] aa cc bb bb cc bb
Levels: bb cc aa

> # First bar = "bb" (3), second bar "cc" (2) and third bar "aa" (1)
> # with 52 levels in the real data frame (many with equal counts)
> # and other similar variables, I hope it is possible to script this
> efficiently
>
> ggplot(df) + geom_bar(aes(v1))

Substituting v2 for v1 in that code seemed to do the trick in ggplot2.

>
> Thank you in advance
>
> Morten
> -- 
> View this message in context: http://r.789695.n4.nabble.com/ggplot2-Pareto-plot-Barplot-in-decreasing-frequency-tp2965796p2965796.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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list