【问题标题】:ggplot2: how to sort the categories in horizontal bar charts?ggplot2:如何对水平条形图中的类别进行排序?
【发布时间】:2017-08-20 10:25:12
【问题描述】:

我很难找到正确的方法在 ggplot 中重新编码我的文本 x 轴。 考虑这个简单的例子:

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

现在是图表

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

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

使用Order Bars in ggplot2 bar graphggplot bar plot with facet-dependent order of categories 中的解决方案没有帮助。

我怀疑这是与水平对齐有关的问题。例如,使用

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

只对一张图表进行排序(在右侧)。

有什么想法吗? 谢谢!

> 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   

【问题讨论】:

  • 我无法让它与这些水平条形图一起使用。我错过了什么吗? @MrFlick?
  • 那么这个副本可能更相关:stackoverflow.com/questions/18624394/…。但最好准确地展示您的尝试。
  • 让我编辑一下
  • @MrFlick 你能重新打开这个问题吗?建议的链接不能解决我的问题。谢谢!
  • 检查forcats,例如fct_reorder()

标签: r ggplot2


【解决方案1】:

我删除了一些无用的部分,例如组,使用“现代化”geom_col(),但诀窍可能是在每个因子级别执行sum,而不是mean,这是reorder 的默认值。始终如一地使用tidyverse 函数通常可以让您避免不愉快的意外,即使reorder 也可以在这里工作。

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

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

【讨论】:

  • 谢谢!但是你能详细说明你为什么在这里使用sum吗?我就是不明白
  • @Noobie 你的两个组都有hello。如果没有sum hello,则它的两个关联counts 中的mean 排名,这比sum 更难预测。同时sum 为您提供忽略(求和)所有组的整体排名。
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 2022-08-23
  • 1970-01-01
相关资源
最近更新 更多