如何使用forcats根据另一个变量的子集(构面)对因子进行重新排序? [英] How to reorder a factor based on a subset (facets) of another variable, using forcats?

查看:147
本文介绍了如何使用forcats根据另一个变量的子集(构面)对因子进行重新排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

forcats 渐晕指出

forcats软件包的目标是提供一套有用的工具 通过因素解决常见问题

The goal of the forcats package is to provide a suite of useful tools that solve common problems with factors

实际上,其中一种工具是通过另一个变量对因子进行重新排序,这是绘制数据时非常常见的用例.我试图使用forcats来完成此操作,但是在多面图的情况下.也就是说,我想通过其他变量对一个因子进行重新排序,但仅使用数据的一个子集.这里是一个代表:

And indeed one of the tools is to reorder factors by another variable, which is a very common use case in plotting data. I was trying to use forcats to accomplish this, but in the case of a faceted plot. That is, I want to reorder a factor by other variable, but using only a subset of the data. Here's a reprex:

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

这段代码产生的图接近我想要的:

This code produces the plot close to what I want:

但是我希望清晰度轴可以按值排序,因此我可以快速找出哪个清晰度值最高.但是,每个方面都意味着不同的顺序.因此,我想选择按特定构面内的值对图进行排序.

But I want the clarity axis to be sorted by value, so I can quickly spot which clarity has the highest value. But then each facet would imply a different order. So I'd like to choose to order the plot by the values within a specific facet.

在这种情况下,直接使用forcats当然是行不通的,因为这将基于所有值(不仅是特定构面的值)对因子进行重新排序.做吧:

The straightforward use of forcats, of course, does not work in this case, 'cause it would reorder the factor based on all the values, and not only the values of a specific facet. Let's do it:

# Inserting this line right before the ggplot call
mutate(clarity = forcats::fct_reorder(clarity, value)) %>%

然后生成此图.

当然,它会基于整个数据对因子进行重新排序,但是如果我想按理想"切割的值对图进行排序,该怎么办?如何使用forcats来做到这一点?

Of course, it reordered the factor based on the whole data, but what if I want the plot ordered by the values of the "Ideal" cut?, How can I do this with forcats?

我当前的解决方案如下:

My current solution would be as follows:

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

哪个生产出我想要的东西.

Which produces what I want.

但是我想知道是否有使用forcats的更优雅/更聪明的方法.

But I wonder if there is a more elegant/clever way to do it using forcats.

推荐答案

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

If you want to reorder clarity by the values of a particular facet you have to tell forcats::fct_reorder() to do so, e.g.,

mutate(clarity = forcats::fct_reorder(
    clarity, filter(., cut == "Ideal") %>% pull(value)))

仅使用理想"构面的值进行重新排序.

which uses only the values for the "Ideal" facet for reordering.

因此

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

创建

根据要求.

这篇关于如何使用forcats根据另一个变量的子集(构面)对因子进行重新排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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