I have a df of public and private schools within counties, and each has an assigned value. I want to use forcats::fct_reorder to rearrange the counties by the median value, but only for the private schools. Using default forcats::fct_reorder arranges by total median, which is less useful for what I'm doing.
Reprex here:
# 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)
Desired output would order them by increasing median of private schools only: Aransas, Travis, Tarrant, Bexar.
thanks!
