I am creating a side-by-side bar chart with ggplot2. My code produces the correct plot when scale_x_discrete(drop = T). However, I have a level with a value of 0, and I want to include this on the x-axis. When I set scale_x_discrete(drop = F), I get the warning: Removed x rows containing missing values (geom_bar). and another category with a non-zero value is shown as zero on the plot.
Here's a reprex of my data:
library("tidyverse")
df <- data.frame(
location = c(rep("in", 231), rep("out", 83)),
status = c(rep("normal", 73), rep("mild", 42), rep("moderate", 20), rep("fever", 4),
rep("normal", 70), rep("mild", 41), rep("moderate", 62), rep("fever", 2)))
df$status <- factor(df$status, levels = c("normal", "mild", "moderate", "severe", "fever"))
df %>%
ggplot(aes(x = status,
y = ..count../tapply(..count.., ..x.., sum)[..x..],
fill = location)) +
geom_bar(position = "dodge") +
scale_y_continuous(labels = scales::percent) +
scale_x_discrete(drop=F) +
NULL
I have been looking at this for ages and really cannot work out the problem.
Can't explain the non-zero values not plotting. Here is a solution using a dplyr's group_by functions
#calculate totals and then calculate the %
df %>% group_by(status, location) %>% summarise(value=n()) %>%
group_by(status) %>% mutate(result=value/sum(value)) %>%.
ggplot(aes(x = status,
y = result,
fill = location)) +
geom_col(position = "dodge") +
scale_y_continuous(labels = scales::percent) +
scale_x_discrete(drop=F)
Notice this now geom_col and not geom_bar.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With