使用 reorder_within 对轴标签进行排序
Posted
技术标签:
【中文标题】使用 reorder_within 对轴标签进行排序【英文标题】:Ordering Axis Labels using reorder_within 【发布时间】:2022-01-08 04:04:00 【问题描述】:我正在尝试使用此链接中描述的“内部重新排序”功能对我的轴标签进行排序:https://juliasilge.com/blog/reorder-within/ 稍作更改 - 将 2 个人的值设置为相同。
这是我正在使用的代码:
library(tidytext)
library(babynames)
library(tidyverse)
top_names <- babynames %>%
filter(year >= 1950,
year < 1990) %>%
mutate(decade = (year %/% 10) * 10) %>%
group_by(decade) %>%
count(name, wt = n, sort = TRUE) %>%
ungroup() %>%
mutate(n = ifelse(decade == "1950" & name == "Michael", 846042, n)) #Added to create 2 rows with a tie.
top_names %>%
group_by(decade) %>%
top_n(15) %>%
ungroup %>%
mutate(decade = as.factor(decade),
name = reorder_within(name, n, decade)) %>%
ggplot(aes(name, n, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~decade, scales = "free_y") +
coord_flip() +
scale_x_reordered() +
scale_y_continuous(expand = c(0,0)) +
labs(y = "Number of babies per decade",
x = NULL,
title = "What were the most common baby names in each decade?",
subtitle = "Via US Social Security Administration")
我能够完美地运行代码,但我的问题是我想颠倒 y 轴上的名称顺序(顶部最不常见,底部最常见)。我可以通过将带有 reorder_within 的行更改为:
mutate(decade = as.factor(decade),
name = reorder_within(name, -n, decade))
但是,这颠倒了“迈克尔和约瑟夫”名字的顺序,我不希望发生这种情况。
如何指定当出现平局时,我希望名称仍按字母顺序排列?
【问题讨论】:
或者,更好的是,在您绘制之前保存您过滤的top_names
并与我们分享dput(filtered_top_names)
。然后我们可以在不安装babynames
包的情况下为您提供帮助。
【参考方案1】:
TBMK reorder_within
不提供不同的选项来处理关系。但是,一种选择是在应用reorder_within
后以所需顺序重新排列数据集,并使用forcats::fct_inorder
(重新)设置因子水平:
library(tidytext)
library(babynames)
library(tidyverse)
top_names %>%
group_by(decade) %>%
top_n(15) %>%
ungroup %>%
mutate(decade = as.factor(decade),
name = reorder_within(name, -n, decade)) %>%
arrange(decade, desc(n), desc(name)) %>%
mutate(name = fct_inorder(name)) %>%
ggplot(aes(name, n, fill = decade)) +
geom_col(show.legend = FALSE) +
geom_text(aes(label = n), hjust = 1) +
facet_wrap(~decade, scales = "free_y") +
coord_flip() +
scale_x_reordered() +
scale_y_continuous(expand = c(0,0)) +
labs(y = "Number of babies per decade",
x = NULL,
title = "What were the most common baby names in each decade?",
subtitle = "Via US Social Security Administration")
#> Selecting by n
【讨论】:
以上是关于使用 reorder_within 对轴标签进行排序的主要内容,如果未能解决你的问题,请参考以下文章