3

forcats ビネットは次のように述べています

forcats パッケージの目的は、因子に関する一般的な問題を解決する一連の便利なツールを提供することです。

実際、ツールの 1 つは、因子を別の変数で並べ替えることです。これは、データをプロットする際の非常に一般的な使用例です。私はforcatsこれを達成するために使用しようとしていましたが、ファセットプロットの場合. つまり、因子を他の変数で並べ替えたいのですが、データのサブセットのみを使用しています。ここにレプレックスがあります:

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ですか?

私の現在の解決策は次のとおりです。

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

4

1 に答える 1