I feel like this should be trivial but I can't figure it out.
I want to reorder the x-axis of one graph based on the output of another dataframe where I've calculated some other variable and arranged it in descending order. So specifically in the below example, I want to reorder the x-axis by order_df
: it should read 2, 3, 1 (which is the descending order of other_var
: 200, 60, 40).
library(tidyverse)
graph_df <- data.frame(
trial = factor(c(1,1,2,2,3,3)),
probability = c(90,10,50,50,10,90),
group = factor(c(1,2,1,2,1,2))
)
ggplot(graph_df, aes(x = trial, y = probability, fill = group)) +
geom_col() +
theme_minimal() +
theme(
legend.position = "bottom"
)
order_df <- data.frame(
trial = c(1, 2, 3),
other_var = c(40, 200, 60)
) %>%
arrange(desc(other_var)) %>%
pull(trial)
ggplot(graph_df, aes(x = fct_reorder(trial, order_df), y = probability, fill = group)) +
geom_col() +
theme_minimal() +
theme(
legend.position = "bottom"
)
#> Error in fct_reorder(trial, order_df): length(f) == length(.x) is not TRUE
Created on 2019-03-18 by the reprex package (v0.2.1)