簡體   English   中英

如何使用 forcats 根據另一個變量的子集(方面)對因子重新排序?

[英]How to reorder a factor based on a subset (facets) of another variable, using forcats?

forcats小插圖指出

forcats 包的目標是提供一套有用的工具來解決常見的因子問題

事實上,其中一個工具是通過另一個變量對因子重新排序,這是繪制數據的一個非常常見的用例。 我試圖使用forcats來實現這一點,但在多面情節的情況下。 也就是說,我想按其他變量對因子重新排序,但只使用數據的一個子集。 這是一個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")

這段代碼產生的情節接近我想要的:

在此處輸入圖片說明

但我希望清晰度軸按值排序,這樣我就可以快速找出哪個清晰度值最高。 但是每個方面都意味着不同的順序。 所以我想選擇按特定方面內的值對繪圖進行排序。

當然,直接使用forcats在這種情況下不起作用,因為它會根據所有值重新排序因子,而不僅僅是特定方面的值。 我們開始做吧:

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

然后它產生這個情節。 在此處輸入圖片說明

當然,它根據整個數據對因子重新排序,但是如果我想要按“理想”切割的值排序的圖怎么辦?,我如何使用forcats做到這forcats

我目前的解決方案如下:

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來做到這forcats

如果您想通過特定方面的值重新排序clarity ,您必須告訴forcats::fct_reorder()這樣做,例如,

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

它僅使用“理想”方面的值進行重新排序。

因此,

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

創造

在此處輸入圖片說明

按照要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM