How do I keep a constant width bar chart in R's ggplot2 when there are redundant levels?

Viewed 2502

Consider some sample data where a level does not occur:

dat <- data.frame(sex = c("F","M","F"),
                  status = c("Pregnant","Not pregnant","Not pregnant"),
                  frequency = c(25,100,75))

In the sample data males cannot become pregnant so they have no data.

I would like to plot the data using ggplot2 in R using this code:

library(ggplot2)

p <- ggplot(dat,aes(x=status,y=frequency,fill=sex))+
  geom_bar(stat = "identity",position="dodge")

print(p)

I get this output:

enter image description here

As you can see, the bar for female and pregnant is wider than the other bars. My question is how can I suppress the bar widening to have the bars the same width?

2 Answers
Related