首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据另一个变量的子集(面)重新排序一个因素,使用forcat?

如何根据另一个变量的子集(面)重新排序一个因素,使用forcat?
EN

Stack Overflow用户
提问于 2019-01-29 22:58:34
回答 1查看 1.2K关注 0票数 3

forcats 格列奈特

forcats包的目标是提供一套有用的工具,用各种因素解决常见的问题。

事实上,其中一个工具是通过另一个变量重新排序因素,这是绘制数据的一个非常常见的用例。我试图使用forcats来实现这一点,但在一个分面的情节中。也就是说,我希望通过其他变量重新排序一个因子,但只使用数据的一个子集。这里有个解释:

代码语言:javascript
复制
library(tidyverse)

ggplot2::diamonds %>% 
    group_by(cut, clarity) %>% 
    summarise(value = mean(table, na.rm = TRUE)) %>%
    ggplot(aes(x = clarity, y = value, color = clarity)) + 
    geom_segment(aes(xend = clarity, y = min(value), yend = value), 
                 size = 1.5, alpha = 0.5) + 
    geom_point(size = 3) + 
    facet_grid(rows = "cut", scales = "free") +
    coord_flip() +
    theme(legend.position = "none")

这段代码产生的情节与我想要的接近:

但是我希望清晰轴按值进行排序,这样我就可以快速发现哪一个清晰度值最高。但是,每个方面都意味着一个不同的顺序。因此,我想选择按一个特定方面内的值来排序这个图。

当然,直接使用forcats在这种情况下是行不通的,因为它将根据所有的值,而不仅仅是特定方面的值,重新排序因子。

代码语言:javascript
复制
# Inserting this line right before the ggplot call
mutate(clarity = forcats::fct_reorder(clarity, value)) %>%

然后它就产生了这个情节。

当然,它根据整个数据重新排序了因子,但是如果我希望按照“理想”裁剪的值排序这个图呢?我如何使用forcats来完成这个任务呢?

我目前的解决办法如下:

代码语言:javascript
复制
ggdf <- ggplot2::diamonds %>% 
    group_by(cut, clarity) %>% 
    summarise(value = mean(table, na.rm = TRUE))

# The trick would be to create an auxiliary factor using only
# the subset of the data I want, and then use the levels
# to reorder the factor in the entire dataset.
#
# Note that I use good-old reorder, and not the forcats version
# which I could have, but better this way to emphasize that
# so far I haven't found the advantage of using forcats 
reordered_factor <- reorder(ggdf$clarity[ggdf$cut == "Ideal"], 
                            ggdf$value[ggdf$cut == "Ideal"])

ggdf$clarity <- factor(ggdf$clarity, levels = levels(reordered_factor))

ggdf %>%
    ggplot(aes(x = clarity, y = value, color = clarity)) + 
    geom_segment(aes(xend = clarity, y = min(value), yend = value), 
                 size = 1.5, alpha = 0.5) + 
    geom_point(size = 3) + 
    facet_grid(rows = "cut", scales = "free") +
    coord_flip() +
    theme(legend.position = "none")

这就产生了我想要的。

但我想知道是否有一种更优雅、更聪明的使用forcats的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-30 06:50:01

如果要根据特定方面的值重新排序clarity,则必须告诉forcats::fct_reorder()这样做,例如,

代码语言:javascript
复制
mutate(clarity = forcats::fct_reorder(
    clarity, filter(., cut == "Ideal") %>% pull(value)))

它只使用“理想”方面的值来重新排序。

因此,

代码语言:javascript
复制
ggplot2::diamonds %>% 
  group_by(cut, clarity) %>% 
  summarise(value = mean(table, na.rm = TRUE)) %>%
  mutate(clarity = forcats::fct_reorder(
    clarity, filter(., cut == "Ideal") %>% pull(value))) %>%
  ggplot(aes(x = clarity, y = value, color = clarity)) + 
  geom_segment(aes(xend = clarity, y = min(value), yend = value), 
               size = 1.5, alpha = 0.5) + 
  geom_point(size = 3) + 
  facet_grid(rows = "cut", scales = "free") +
  coord_flip() +
  theme(legend.position = "none")

创建

应要求。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54430898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档