Geom_bar + facet_grid not behaving as expected

ggplot2

#1

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.

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

#2

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)

with

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

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.


#3

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?
https://drive.google.com/file/d/19gkiutbEczba9Cgjn5FS7y2OxH6u3mfr/view?usp=sharing