ggplot2:如何对水平条形图中的类别进行排序? [英] ggplot2: how to sort the categories in horizontal bar charts?

查看:90
本文介绍了ggplot2:如何对水平条形图中的类别进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到正确的方法在ggplot中整理文本 x 轴.考虑这个简单的例子:

 库(ggplot2)图书馆(dplyr)dataframe<-data_frame('group'= c(1,1,1,2,2,2),'text'= c('hello','world','nice','hello','magic','bug'),'计数'= c(12,10,3,4,3,2))>数据框#小动作:6×3组文字数< dbl>< chr>< dbl>1 1你好122 1世界103 1不错34 2你好45 2魔法36 2错误2 

现在是图表

  ggplot(数据框,aes(x =文本,y =计数,填充=计数,组=组))+geom_bar(stat ='identity')+facet_wrap(〜group,scales ="free_y")+coord_flip() 

问题是:我想按递增的 count 顺序对单词进行排序,以使计数最高的单词出现在每个类别的底部.

使用

有什么想法吗?谢谢!

 >sessionInfo()R版本3.3.2(2016-10-31)平台:x86_64-w64-mingw32/x64(64位)在以下环境中运行:Windows 7 x64(内部版本7601)Service Pack 1语言环境:[1] LC_COLLATE =英语_美国.1252 LC_CTYPE =英语_美国.1252[3] LC_MONETARY =英语_美国.1252LC_NUMERIC = C[5] LC_TIME =英语_美国.1252附带的基本软件包:[1]统计图形grDevices utils数据集方法库其他附件包:[1] dplyr_0.5.0 ggplot2_2.2.1通过名称空间(未附加)加载:[1] Rcpp_0.12.9摘要_0.6.12断言_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秤_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 

解决方案

我删除了一些无用的部分,例如组,使用了'modernized' geom_col(),但是窍门可能在于执行每个要素级别的> sum 而不是 mean (这是 reorder 的默认设置).始终使用 tidyverse 函数通常可以使您免于意外的困扰,即使 reorder 在这里可以正常工作也是

 库(tidyverse)数据框%&%;%mutate(文本=文本%>%forcats :: fct_reorder(count,sum))%&%;%ggplot(aes(x =文本,y =计数,填充=计数))+geom_col()+facet_wrap(〜group,scales ="free_y")+coord_flip() 

请记住,因子只有一个排序,这意味着,如果您相应地处理数据(即,每个方面afaik没有排序),则在两个方面中您可以进行相反的排序.

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() 

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).

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   

解决方案

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).

这篇关于ggplot2:如何对水平条形图中的类别进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆