Ggplot2: plot sorting

I have a data.frame which is sorted in ascending order. For example:

x <- structure(list(variable = structure(c(10L, 6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), class = c("ordered", "factor")), value = c(0.990683229813665, 0.975155279503106, 0.928571428571429, 0.807453416149068, 0.717391304347826, 0.388198757763975, 0.357142857142857, 0.201863354037267, 0.173913043478261, 0.0496894409937888)), .Names = c("variable", "value"), row.names = c(10L, 6L, 3L, 4L, 2L, 8L, 9L, 5L, 1L, 7L), class = "data.frame") ggplot(x, aes(x=variable,y=value)) + geom_bar(stat="identity") + scale_y_continuous("",label=scales::percent) + coord_flip() 

Now the data is good and sorted, but when I build the graphs, they are sorted by factor. This is annoying, how can I fix it?

+47
r ggplot2
Sep 19 '10 at 1:29
source share
5 answers

Here are some ways.

The first will order things based on the order observed in the data frame:

 x$variable <- factor(x$variable, levels=unique(as.character(x$variable)) ) 

The second orders levels based on another variable (value in this case):

 x <- transform(x, variable=reorder(variable, -value) ) 
+55
Sep 19 '10 at 3:31
source share

This is similar to what you are looking for:

 g <- ggplot(x, aes(reorder(variable, value), value)) g + geom_bar() + scale_y_continuous(formatter="percent") + coord_flip() 

The reorder() function will reorder your x-axis elements according to the value variable .

+69
Sep 21 '10 at 8:24
source share

Recently, I struggled with a related issue discussed in detail here: Ordering legend entries in ggplot2 lines with coordinate () .

Be that as it may, the reason I had difficulty explaining my problem was in the relationship between (order) of factors and coord_flip (), as is the case here.

I get the desired result by adding + xlim(rev(levels(x$variable))) to the ggplot operator:

 ggplot(x, aes(x=variable,y=value)) + geom_bar() + scale_y_continuous("",formatter="percent") + coord_flip() + xlim(rev(levels(x$variable))) 

This changes the order of the coefficients found in the original data frame along the x axis, which will become the y axis with the coordinate. Note that in this particular example, the variable is also in alphabetical order, but specifying an arbitrary order of levels inside xlim() should work at all.

+9
Sep 05 '11 at 16:41
source share

You need to make the x-factor into the ordered coefficient with the desired order, for example

 x <- data.frame("variable"=letters[1:5], "value"=rnorm(5)) ## example data x <- x[with(x,order(-value)), ] ## Sorting x$variable <- ordered(x$variable, levels=levels(x$variable)[unclass(x$variable)]) ggplot(x, aes(x=variable,y=value)) + geom_bar() + scale_y_continuous("",formatter="percent") + coord_flip() 

I do not know a better way to execute an order operation. What I have will only work if there are no duplicate levels for x$variable .

+2
Sep 19 '10 at 1:40
source share

I do not know why this question was reopened, but there is an option with tidyverse change tidyverse .

 x %>% arrange(desc(value)) %>% mutate(variable=fct_reorder(variable,value)) %>% ggplot(aes(variable,value,fill=variable)) + geom_bar(stat="identity") + scale_y_continuous("",label=scales::percent) + coord_flip() 
0
Dec 18 '18 at 11:53
source share



All Articles