Geom_bar + facet_grid not behaving as expected



In the this ggplot I generated, I think the width of the rectangle in facet == 3 is not right. I want it the be equally wide as other rectangles and should be in the middle of Sex == 2 and Sex == 4 respectively. Did I do anything wrong?

A reproducible example will be as follow. Unfortunately I couldn't find the option of uploading files in the post but I will be happy to share with emails.

ggplot(b, aes(x = Sex), weight = rel_mean) + geom_bar(position = "fill") + 
    facet_grid(rows = vars(Race))


If you look for information on the function geom_bar with ?geom_bar, you will see that the defaults arguments for this function are as follows:

 geom_bar(mapping = NULL, data = NULL, stat = "count",
       position = "stack", ..., width = NULL, binwidth = NULL, na.rm = FALSE,
       show.legend = NA, inherit.aes = TRUE)


  width: Bar width. By default, set to 90% of the resolution of the

So I imagine that your 3rd graph has a different data resolution? If you want all the bars to be of the same width, set a numerical value for the width argument in the geom_bar function:

ggplot(b, aes(x = Sex), weight = rel_mean) +
    geom_bar(position = "fill", width = n) + 
    facet_grid(rows = vars(Race))

(and replace n by whatever value works for you of course).

I cannot test it without the data, so please let us know if this fixes your problem.


Thank you prosoitos! I tried using different widths but they did not adjust the specific bar that I wanted, and here is the data, could you take a look?