Sort Stacked Bar Chart

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.

The forcats::fct_reorder function takes .fun as parameter, rather than fun:

df <- df %>% mutate(factors2 = forcats::fct_reorder(factors2, as.numeric(factors1), .fun = mean))  
#> Warning in stopifnot(length(f) == length(.x)): NAs introduced by coercion

This will not work however, as factors1 is a character vector that can't be read as numeric. You could try reordering to the data column instead:

df <- df %>% mutate(factors2 = forcats::fct_reorder(factors2, data, .fun = mean)) 

This orders the factors2 variable by the mean value of data in each group. If you want to do it by the sum instead, it should be:

df <- df %>% mutate(factors2 = forcats::fct_reorder(factors2, data, .fun = sum)) 

The second line of the ggplot defines the stat as both summary and identity:

ggplot2::ggplot(df, aes(x = factors2, y = data, fill = factors1)) +
  stat_summary(geom = "bar", position = "stack", stat = "identity")
#> Warning: Ignoring unknown parameters: stat
#> No summary function supplied, defaulting to `mean_se()`


Note how the y-axis gives the mean of each group rather than identity. Drawing the mean is the standard behaviour of stat_summary. A common way of making a bar chart showing identity instead is:

ggplot2::ggplot(df, aes(x = factors2, y = data, fill = factors1)) +
  geom_bar(stat = "identity")
1 Like

awesome, the main thing I was missing was the .fun sum, and careless use of numeric & identity . Took me just a couple minutes to resolve my code and everything works great. Thanks!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.