I have the following data.frame I would like to sort for ordered visualization in a bar or column chart:
factors1 <- c("A","A","A","A","B","B","B","B")
factors2 <- c("w","x","w","z","w","x","y","z")
data <- c(1,3,5,7,5,4,3,2)
df <- data.frame(factors1,factors2,data)
df
p <- ggplot2::ggplot(df, aes(x = factors2, y = data, fill = factors1))+
stat_summary(geom = "bar",position="stack", stat="identity")
p
I would like to sort the stacked bar graph by sum when data is grouped by factors2.
I tried this link and solution but continue to get errors link:
df <- df %>% mutate(factors2 = forcats::fct_reorder(factors2, as.numeric(factors1), fun = mean))
Error: Problem with mutate()
input factors2
.
x 1 components of ...
were not used.
We detected these problematic arguments:
fun
Did you misspecify an argument?
i Input factors2
is forcats::fct_reorder(factors2, as.numeric(factors1), fun = mean)
any suggestions are appreciated.