Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a Pareto Chart with ggplot2 and R
    primarykey
    data
    text
    <p>I have been struggling with how to make a <a href="http://en.wikipedia.org/wiki/Pareto_chart" rel="noreferrer">Pareto Chart</a> in R using the ggplot2 package. In many cases when making a bar chart or histogram we want items sorted by the X axis. In a Pareto Chart we want the items ordered descending by the value in the Y axis. Is there a way to get ggplot to plot items ordered by the value in the Y axis? I tried sorting the data frame first but it seems ggplot reorders them. </p> <p>Example:</p> <pre><code>val &lt;- read.csv("http://www.cerebralmastication.com/wp-content/uploads/2009/11/val.txt") val&lt;-with(val, val[order(-Value), ]) p &lt;- ggplot(val) p + geom_bar(aes(State, Value, fill=variable), stat = "identity", position="dodge") + scale_fill_brewer(palette = "Set1") </code></pre> <p>the data frame val is sorted but the output looks like this:</p> <p><a href="http://www.cerebralmastication.com/wp-content/uploads/2009/11/exp.png" rel="noreferrer">alt text http://www.cerebralmastication.com/wp-content/uploads/2009/11/exp.png</a></p> <p>Hadley correctly pointed out that this produces a much better graphic for showing actuals vs. predicted:</p> <pre><code>ggplot(val, aes(State, Value)) + geom_bar(stat = "identity", subset = .(variable == "estimate"), fill = "grey70") + geom_crossbar(aes(ymin = Value, ymax = Value), subset = .(variable == "actual")) </code></pre> <p>which returns:</p> <p><a href="http://www.cerebralmastication.com/wp-content/uploads/2009/11/exp1.png" rel="noreferrer">alt text http://www.cerebralmastication.com/wp-content/uploads/2009/11/exp1.png</a></p> <p>But it's still not a Pareto Chart. Any tips?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload