Ggplot2 site sorting Part II

I have a molten data.frame, dput (x), below:

## dput(x) x <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("a", "b", "c", "d"), class = "factor"), value = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Never Heard of", "Heard of but Not at all Familiar", "Somewhat Familiar", "Familiar", "Very Familiar", "Extremely Familiar" ), class = "factor"), freq = c(10L, 24L, 32L, 90L, 97L, 69L, 15L, 57L, 79L, 94L, 58L, 19L, 11L, 17L, 34L, 81L, 94L, 85L, 4L, 28L, 59L, 114L, 82L, 35L)), .Names = c("variable", "value", "freq" ), row.names = c(NA, -24L), class = "data.frame") 

What looks like this (for those of you who don't need a test suite):

  variable value freq 1 a Never Heard of 10 2 a Heard of but Not at all Familiar 24 3 a Somewhat Familiar 32 4 a Familiar 90 5 a Very Familiar 97 6 a Extremely Familiar 69 7 b Never Heard of 15 8 b Heard of but Not at all Familiar 57 9 b Somewhat Familiar 79 10 b Familiar 94 11 b Very Familiar 58 12 b Extremely Familiar 19 13 c Never Heard of 11 14 c Heard of but Not at all Familiar 17 15 c Somewhat Familiar 34 16 c Familiar 81 17 c Very Familiar 94 18 c Extremely Familiar 85 19 d Never Heard of 4 20 d Heard of but Not at all Familiar 28 21 d Somewhat Familiar 59 22 d Familiar 114 23 d Very Familiar 82 24 d Extremely Familiar 35 

Now I can make a nice and beautiful plot that looks like this:

 ggplot(x, aes(variable, freq, fill = value)) + geom_bar(position = "fill") + coord_flip() + scale_y_continuous("", formatter="percent") 

Question

What I would like to do is sort a, b, c, d by highest to lowest frequency "Extremely Familiar"

?relevel and ?reorder did not provide constructive examples for this use.

Your help is always appreciated.

Greetings

VEV

+1
r ggplot2
Sep 24 '10 at 21:13
source share
2 answers

Here is one way:

 tmpfun <- function(i) { tmp <- x[i,] -tmp[ tmp$value=='Extremely Familiar', 'freq' ] } x$variable <- reorder( x$variable, 1:nrow(x), tmpfun ) 
+1
Sep 24 '10 at 21:50
source share

Here is another way to do this:

 tmp <- subset(x, value=="Extremely Familiar") x$variable <- factor(x$variable, levels=levels(x$variable)[order(-tmp$freq)]) 
+3
Sep 24 '10 at 22:00
source share



All Articles