fct_reorder plotting trouble (reprex included)

I'm having trouble getting my bars in the correct order. I tried ungrouping but I just have no idea what's causing this error. Any help would be appreciated

library(tidyverse)
structure(list(
  estimate = c(11.111247470798, 10.4662218220111, 
9.87286101475029, 8.96746112935442, 8.92108170016026, 7.08832829986602, 
7.04818865012549, 6.62726882286672, 6.4016658616157, 5.90243717932255, 
-8.06613096601733, -8.2327856904955, -8.29332173766532, -8.54770470136022, 
-8.82071952004217, -9.3058109157512, -9.46824772100271, -10.2010370607916, 
-12.1533813085304, -13.1205784952985), term = c("player", "day", 
"1", "improve", "hardest", "initial", "sophomore", "extremely", 
"player", "player", "finish", "dont", "fair", "limit", "expect", 
"solid", "degree", "degree", "dont", "ran"), 
direction = c("Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Negative")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
"data.frame")) %>% 
    mutate(term = fct_reorder(term, estimate, max)) %>%
  ggplot(aes(term, estimate, fill = direction)) +
  geom_col(show.legend = F) +
  coord_flip()

Created on 2020-07-18 by the reprex package (v0.3.0)

What would be the correct order for you? have in mind that you have more than one value for each "term" level so you are getting stacked columns, maybe you forgot to summarise the data? This example will make it evident.

library(tidyverse)
sample_df <- data.frame(
  stringsAsFactors = FALSE,
          estimate = c(11.111247470798,
                       10.4662218220111,9.87286101475029,8.96746112935442,8.92108170016026,
                       7.08832829986602,7.04818865012549,6.62726882286672,
                       6.4016658616157,5.90243717932255,-8.06613096601733,
                       -8.2327856904955,-8.29332173766532,-8.54770470136022,
                       -8.82071952004217,-9.3058109157512,-9.46824772100271,
                       -10.2010370607916,-12.1533813085304,-13.1205784952985),
              term = c("player","day","1","improve",
                       "hardest","initial","sophomore","extremely",
                       "player","player","finish","dont","fair","limit","expect",
                       "solid","degree","degree","dont","ran"),
         direction = c("Positive","Positive",
                       "Positive","Positive","Positive","Positive","Positive",
                       "Positive","Positive","Positive","Negative","Negative",
                       "Negative","Negative","Negative","Negative","Negative",
                       "Negative","Negative","Negative")
)

sample_df %>% 
    mutate(term = fct_reorder(term, estimate, sum)) %>%
    ggplot(aes(estimate, term, color = direction)) +
    geom_col(show.legend = F)

Created on 2020-07-19 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.