fct_reorder 按功能仅针对一组

fct_reorder by function for only one group

我有一个 public 的 df 和县内的私立学校,每个都有一个指定的值。我想使用 forcats::fct_reorder 按中值重新排列县,但仅限于私立学校。使用默认 forcats::fct_reorder 按总中位数排列,这对我正在做的事情用处不大。

此处代表:

# make df
set.seed(1)
df <-
  data.frame(
    county = rep(c("Bexar","Travis","Tarrant","Aransas"), each=20),
    type = rep(c("public","private"), each=10)
  ) %>%
  mutate(value = case_when(type == "public" ~ runif(80,0,1),
                           type == "private" ~ runif(80, 0, 10))) 
# private values are way higher than public


# relevel by median value
df %>%
  mutate(county = forcats::fct_reorder(county, value, .fun=median)) %>% 
  # this rearranges counties by total median, but I only want to arrange by median of the private schools
  
  # plot
  ggplot(aes(x=county, y = value, color = type)) +
  geom_point(position = position_dodge(
    width=.75
  )) +
  geom_boxplot(alpha=.5)

期望的产出将通过增加私立学校的中位数来排序:Aransas、Travis、Tarrant、Bexar。

谢谢!

library(tidyverse)

set.seed(1)

df <-
  data.frame(
    county = rep(c("Bexar","Travis","Tarrant","Aransas"), each=20),
    type = rep(c("public","private"), each=10)
  ) %>%
  mutate(value = case_when(type == "public" ~ runif(80,0,1),
                           type == "private" ~ runif(80, 0, 10))) 

private_medians <-
  df %>%
  filter(type == "private") %>%
  group_by(county) %>%
  summarise(median = median(value)) %>%
  arrange(median)
private_medians
#> # A tibble: 4 x 2
#>   county  median
#>   <chr>    <dbl>
#> 1 Aransas   3.91
#> 2 Travis    4.39
#> 3 Tarrant   5.68
#> 4 Bexar     6.24

# add other counties at the end in case they do not appear in the private subset
levels <- private_medians$county %>% union(df$county %>% unique())

df %>%
  mutate(county = county %>% factor(levels = levels)) %>%
  ggplot(aes(x=county, y = value, color = type)) +
  geom_point(position = position_dodge(
    width=.75
  )) +
  geom_boxplot(alpha=.5)

reprex package (v2.0.1)

于 2021-10-18 创建