[R] GGPLOT2: Reverse order of legend to match order of x-axis

Ryan Garner ryan.steven.garner at gmail.com
Thu Mar 25 00:12:28 CET 2010


Hadley, it doesn't work. Here is my step by step sequence following the
Wordpress site instructions.

R version 2.10.1 (2009-12-14)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(ggplot2)
Loading required package: proto
Loading required package: grid
Loading required package: reshape
Loading required package: plyr
Loading required package: digest
> ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()
http://n4.nabble.com/file/n1689957/plot1.jpeg 
> levels(diamonds$cut)
[1] "Fair"      "Good"      "Very Good" "Premium"   "Ideal"    
> diamonds$cut <- factor(diamonds$cut, levels = rev(levels(diamonds$cut)))
> levels(diamonds$cut)
[1] "Ideal"     "Premium"   "Very Good" "Good"      "Fair"
http://n4.nabble.com/file/n1689957/plot2.jpeg 
> ggplot(diamonds, aes(clarity, fill = cut, order = -as.numeric(cut))) +
> geom_bar()
http://n4.nabble.com/file/n1689957/plot3.jpeg 
> diamonds$cut <- factor(diamonds$cut, levels = rev(levels(diamonds$cut)))
> ggplot(diamonds, aes(clarity, fill = cut, order = -as.numeric(cut))) +
> geom_bar()
http://n4.nabble.com/file/n1689957/plot4.jpeg 
-- 
View this message in context: http://n4.nabble.com/GGPLOT2-Reverse-order-of-legend-to-match-order-of-x-axis-tp1689526p1689957.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list