0
votes

I have a hard time finding the correct way to reoder my text x axis in ggplot. Consider this simple example:

library(ggplot2)
library(dplyr)
dataframe <- data_frame('group' = c(1,1,1,2,2,2),
                        'text' = c('hello', 'world', 'nice', 'hello', 'magic', 'bug'),
                        'count' = c(12,10,3,4,3,2))

> dataframe
# A tibble: 6 × 3
  group  text count
  <dbl> <chr> <dbl>
1     1 hello    12
2     1 world    10
3     1  nice     3
4     2 hello     4
5     2 magic     3
6     2   bug     2

and now the chart

ggplot(dataframe, aes(x = text, y = count, fill = count, group = group)) + 
  geom_bar(stat = 'identity') +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip() 

enter image description here

Problem is: I would like to sort the words in increasing count order, so that the word with the highest count appears on the bottom for each category.

Using the solutions in Order Bars in ggplot2 bar graph and ggplot bar plot with facet-dependent order of categories does not help.

I suspect this is a problem related to the horizontal alilgnment. For instance, using

ggplot(dataframe, aes(x = reorder(text, -count), y = count, fill = count, group = group)) + 
  geom_bar(stat = 'identity') +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()

only sorts one chart (on the right).

enter image description here

Any ideas? Thanks!

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.5.0   ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9      digest_0.6.12    assertthat_0.1   grid_3.3.2       plyr_1.8.4      
 [6] R6_2.2.0         gtable_0.2.0     DBI_0.5-1        magrittr_1.5     scales_0.4.1    
[11] lazyeval_0.2.0   labeling_0.3     tools_3.3.2      munsell_0.4.3    colorspace_1.3-2
[16] tibble_1.2   
1
I cannot get it to work with these horizontal bar charts. Am I missing something? @MrFlick?ℕʘʘḆḽḘ
Then maybe this duplicate is more relevant: stackoverflow.com/questions/18624394/…. But it's best to show exactly what you tried.MrFlick
let me edit thatℕʘʘḆḽḘ
@MrFlick can you please reopen the question? the link suggested do not solve my problem. Thanks!ℕʘʘḆḽḘ
check forcats, eg fct_reorder()liborm

1 Answers

2
votes

I removed some useless parts like the group, used 'modernized' geom_col(), but the trick was probably in doing sum per factor level instead of mean, which is the default for reorder. Consistently using the tidyverse functions usually saves you from unpleasant surprises, even if reorder would work here as well.

library(tidyverse)

dataframe %>%
  mutate(text = text %>% forcats::fct_reorder(count, sum)) %>%
  ggplot(aes(x = text, y = count, fill = count)) + 
  geom_col() +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip() 

Keep in mind that there is only one ordering of the factor, which means that in the two facets you can have opposite sorting, if you craft your data accordingly (ie there is no sort per facet afaik).