【发布时间】: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 graph 和ggplot 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()