1

I have a melted 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")

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

   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 pretty plot akin to 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 the highest to lowest "freq" of "Extremely Familiar"

?relevel and ?reorder haven't provided any constructive examples for this usage.

Your help, is always appreciated.

Cheers,

BEB

2 Answers 2

3

Here is another way to do it:

tmp <- subset(x, value=="Extremely Familiar")
x$variable <- factor(x$variable, levels=levels(x$variable)[order(-tmp$freq)])
Sign up to request clarification or add additional context in comments.

Comments

1

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 )

2 Comments

It's so close! Is there a way to reverse this ordering? (Adding a minus sign somewhere?) When I use coord_flip() it sorts it in the opposite direction.
To reverse the ordering remove the minus sign in the definition of tmpfun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.