# Coord_flip not adjusting based on factor level

For the below overlay bar chart I am a bit confused on why I cant order it so that female is above male. Currently my research has shown that leveraging mutate I can create a factored column, and simply pass the desired levels. However, no matter the order I am still getting male on top and female on bottom.

# Data

clm <- c("covid","covid","all","all")
sex <- c("female","male","female","male")
percent <- c(0.6, 0.2, .4, .8)

# Colors

dark <- '#008AFE' # blue
lightest <-'#CCE8FF'
light_accent <-'#FFCB93' #peachy
lightest_accent <- '#FFE5C9'

df <- data.frame("clm" = clm,"sex" = sex, "percent" = percent)

gender_claim <- df %>%
arrange(percent) %>%
mutate(sex=factor(sex, levels = c("male","female"))) %>%
ggplot(aes(x = sex, y = percent)) +
geom_col(data = df[(df\$sex == "female") &
(df\$clm == "all"), ],
fill = lightest_accent, width = .25) +
geom_col(data = df[(df\$sex == "female") &
(df\$clm == "covid"), ],
fill = dark, width = .15) +
geom_col(data = df[(df\$sex == "male") &
(df\$clm == "all"), ],
fill = lightest_accent, width = .25) +
geom_col(data = df[(df\$sex == "male") &
(df\$clm == "covid"), ],
fill = dark, width = .15) +
scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = .25),
labels = function(y) paste0(round(y*100,0),"%"),
seq(0, 1, by = .25),expand = expansion(mult = c(0, 0))) +
coord_flip()

This isn't exactly what you wanted but I hope it shows you a path to get there.

library(dplyr, warn.conflicts = FALSE)
library(ggplot2)
clm <- c("covid","covid","all","all")
sex <- c("female","male","female","male")
percent <- c(0.6, 0.2, .4, .8)

# Colors

dark <- '#008AFE' # blue
lightest <-'#CCE8FF'
light_accent <-'#FFCB93' #peachy
lightest_accent <- '#FFE5C9'

df <- data.frame("clm" = clm,"sex" = sex, "percent" = percent)

df %>%
arrange(clm) %>%
mutate(sex=factor(sex, levels = c("male","female")),
W = ifelse(clm == "covid", 0.15, 0.25)) %>%
ggplot(aes(x = sex, y = percent, fill = clm, width = W)) +
geom_col(position = "identity") +
scale_fill_manual(values = c(all = '#FFE5C9', covid = '#008AFE')) +
scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = .25),
labels = function(y) paste0(round(y*100,0),"%"),
seq(0, 1, by = .25),expand = expansion(mult = c(0, 0))) +
coord_flip()

Created on 2020-12-14 by the reprex package (v0.3.0)

Am I interpreting the following correctly? Since my data is in long format ggplot builds the data row by row. Where each row represents a unique observation. So when you are creating the dummy column W to account for the width logic. The bars are getting built piece by piece.

I do not know anything about this beyond what I learned making the plot in this post. I had to use arrange(clm) to avoid having the clm = "all" bars hide the covid bars. That is consistent with your description of the drawing process.

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.