reorder_within() 神秘地没有在我的数据的方面内排序

问题描述 投票:0回答:1

我一定错过了一些简单的东西,但我一生都无法确定为什么我无法在自己的数据上重现

tidytext::reorder_within()
的行为。

虹膜数据一切正常。对于我的数据,预期输出是 Supercross 圈速应按 event_city 每个方面内骑手圈速中位数排序。具体来说,塞克斯顿应该在阿纳海姆 1 和阿灵顿比赛中都排在最上面,因为他在两场比赛中的中位圈速都较高。但正如您所看到的,劳伦斯错误地位于阿灵顿的第一排。

任何人都可以看到我的代码或数据与虹膜数据有何不同,或者我的代码可能在哪里出错?

library(tidyverse)
library(tidytext)
library(ggpubr)

plot_dat <- tribble(
        ~race_lap,   ~race_type, ~event_track, ~rider_number, ~rider_name,    ~rider_lap_time, ~rider_class,
        23L, "Main Event",  "Arlington",           18L,  "Lawrence",  0.906416666666667,         450L,
        22L, "Main Event",  "Arlington",           18L,  "Lawrence",             0.7854,         450L,
        18L, "Main Event",  "Anaheim 1",           18L,  "Lawrence",             1.0789,         450L,
        6L, "Main Event",  "Arlington",           18L,  "Lawrence",  0.759816666666667,         450L,
        19L, "Main Event",  "Arlington",           18L,  "Lawrence",  0.780033333333333,         450L,
        2L, "Main Event",  "Anaheim 1",           18L,  "Lawrence",             1.0392,         450L,
        24L, "Main Event",  "Arlington",           18L,  "Lawrence",  0.782866666666667,         450L,
        25L, "Main Event",  "Arlington",           96L,  "Lawrence",  0.785966666666667,         450L,
        10L, "Main Event",  "Arlington",           96L,  "Lawrence",             0.7757,         450L,
        20L, "Main Event",  "Arlington",           96L,  "Lawrence",  0.790683333333333,         450L,
        16L, "Main Event",  "Arlington",           18L,  "Lawrence",            0.77565,         450L,
        20L, "Main Event",  "Arlington",            1L,    "Sexton",  0.789516666666667,         450L,
        11L, "Main Event",  "Arlington",           96L,  "Lawrence",  0.776866666666667,         450L,
        8L, "Main Event",  "Anaheim 1",            1L,    "Sexton",   1.05653333333333,         450L,
        5L, "Main Event",  "Arlington",           18L,  "Lawrence",            0.76185,         450L,
        19L, "Main Event",  "Arlington",           96L,  "Lawrence",             0.8057,         450L,
        22L, "Main Event",  "Arlington",            1L,    "Sexton",  0.820566666666667,         450L,
        6L, "Main Event",  "Arlington",            1L,    "Sexton",  0.755516666666667,         450L,
        7L, "Main Event",  "Anaheim 1",            1L,    "Sexton",   1.05981666666667,         450L,
        17L, "Main Event",  "Anaheim 1",            1L,    "Sexton",   1.08501666666667,         450L
)

iris %>% 
        pivot_longer(cols = -Species, names_to = "flower_attribute") %>% 
        ggplot(aes(x = reorder_within(Species, by = value, within = flower_attribute, fun = median), y = value)) +
        geom_point(stat = "summary") +
        geom_errorbar(stat = "summary", fun.data = "median_mad", width = 0.1) +
        geom_line(aes(group = 1), stat = "summary", fun.y = "median_mad") +
        scale_x_reordered() +
        coord_flip() +
        facet_wrap(~ flower_attribute, scales = "free") +
        labs(x = "Species") +
        theme_minimal() 
#> Warning in geom_line(aes(group = 1), stat = "summary", fun.y = "median_mad"):
#> Ignoring unknown parameters: `fun.y`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`


plot_dat %>% 
        ggplot(aes(x = reorder_within(rider_name, by = rider_lap_time, within = event_track), y = rider_lap_time)) +
        geom_point(stat = "summary", fun.data = "median_mad", position = position_nudge(x = 0.15)) +
        geom_errorbar(stat = "summary", fun.data = "median_mad", position = position_nudge(x = 0.15), width = 0.1) +
        geom_line(stat = "summary", fun.data = "median_mad", aes(group = 1), position = position_nudge(x = 0.15)) +
        scale_x_reordered() +
        coord_flip() +
        facet_wrap(~ event_track, scales = "free") +
        labs(x = "Rider", y = "Lap Time (Minutes)") +
        theme_minimal() 

创建于 2024 年 12 月 23 日,使用 reprex v2.1.1

r ggplot2 facet tidytext
1个回答
0
投票

看起来您忘记指定您想要 reorder_within 使用的函数。添加

fun=median
返回下面的图表,听起来就像您所期望的那样。

plot_dat %>% 
  ggplot(aes(x = reorder_within(rider_name, by = rider_lap_time, within = event_track, fun=median), y = rider_lap_time)) +
  geom_point(stat = "summary", fun.data = "median_mad", position = position_nudge(x = 0.15)) +
  geom_errorbar(stat = "summary", fun.data = "median_mad", position = position_nudge(x = 0.15), width = 0.1) +
  geom_line(stat = "summary", fun.data = "median_mad", aes(group = 1), position = position_nudge(x = 0.15)) +
  scale_x_reordered() +
  coord_flip() +
  facet_wrap(~ event_track, scales = "free") +
  labs(x = "Rider", y = "Lap Time (Minutes)") +
  theme_minimal()

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.