1

I want to have the same bar width using facet_wrap. I looked at answers like: How to automatically adjust the width of each facet for facet_wrap? and facet_wrap with equal bar widths but do not achieve the desired result.

Furthermore, I adapted my code and data.frame according to facet_wrap with equal bar widths

My data.frame :

structure(list(strain = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), levels = c("ATCC", "rfs1", "rfs2", "rfs3"
), class = "factor"), medium = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L), levels = c("lim", "ynb", "ypg"), class = "factor"), 
    iron = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L), levels = c("fecl", "feso", "pur"), class = "factor"), 
    concentration = structure(c(2L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 2L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 2L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 2L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 2L, 3L, 4L, 1L, 1L), levels = c("0", "0.2µM", "200µM", 
    "2mM"), class = "factor"), mean24 = c(20.3866666666667, 18.1833333333333, 
    3.115, 20.7216666666667, 19.3233333333333, 7.455, NaN, 9.69666666666667, 
    15.93, 17.5066666666667, 10.48, 13.145, 18.6, 30.0333333333333, 
    31.83, 18.3766666666667, 17.8666666666667, 7.13, 18.1033333333333, 
    18.4416666666667, 3.40166666666667, NaN, 8.47666666666667, 
    13.04, 15.2583333333333, 11.0383333333333, 13.5933333333333, 
    17.5483333333333, 27.3016666666667, 29.3133333333333, 20.3183333333333, 
    20.9316666666667, 3.59, 19.675, 18.7516666666667, 8.61666666666667, 
    NaN, 10.35, 14.8766666666667, 17.1916666666667, 13.3833333333333, 
    17.1333333333333, 20.4066666666667, 31.4233333333333, 33.26, 
    24.2883333333333, 23.315, 3.62166666666667, 23.5916666666667, 
    23.0066666666667, 7.51833333333333, NaN, 12.4666666666667, 
    18.19, 19.0416666666667, 18.3183333333333, 19.5733333333333, 
    22.3733333333333, 39.0616666666667, 35.8016666666667), sd24 = c(0.615976190016031, 
    0.441935138529023, 0.244356297238275, 1.42914543230095, 1.32616238321959, 
    0.237802439011882, NA, 0.682045941756614, 0.547868597384446, 
    0.746958276389428, 0.908735385026907, 0.591700937974582, 
    0.500719482345155, 0.792178431078925, 0.808022276920629, 
    1.05407147132757, 0.227742544700516, 0.501557573963349, 0.538058237244507, 
    0.444585949695519, 0.317453408654982, NA, 0.330252428706689, 
    0.469425180406846, 0.38007455408994, 0.387061579941315, 0.738232122483617, 
    0.622717164262128, 0.498333890746622, 1.15321579362523, 0.500096657324028, 
    0.947574095607656, 0.354570162309239, 0.719909716561737, 
    1.37201190471026, 0.241881513693517, NA, 0.368673297107344, 
    0.805025879501191, 0.499776616766598, 0.883530795539503, 
    0.855912768140929, 0.653442167805741, 1.74278703996405, 1.39828466343588, 
    0.862216136862832, 2.13544140636075, 0.20970614360735, 0.98649717012603, 
    1.17777190774219, 0.969812696692855, NA, 1.21369957842403, 
    1.81666727828736, 1.13312694199135, 1.87879127810054, 1.4216422428539, 
    0.897923530522877, 3.11314257088664, 0.518591039902027), 
    mean48 = c(36.5733333333333, 32.4883333333333, 18.8266666666667, 
    37.0666666666667, 36.07, 5.175, NaN, 40.686, 38.9333333333333, 
    21.8583333333333, 41.4266666666667, 35.3866666666667, 26.64, 
    NaN, NaN, 33.28, 30.865, 18.5316666666667, 34.3133333333333, 
    30.86, 5.67833333333333, NaN, 38.1633333333333, 36.2666666666667, 
    19.5416666666667, 40.745, 35.6683333333333, 27.5833333333333, 
    NaN, NaN, 35.34, 34.1333333333333, 20.14, 35.5, 34.9433333333333, 
    4.83666666666667, NaN, 40.7116666666667, 38.4316666666667, 
    23.485, 42.8916666666667, 40.5916666666667, 31.7183333333333, 
    NaN, NaN, 37.2233333333333, 35.9333333333333, 20.7616666666667, 
    37.4366666666667, 36.395, 5.415, NaN, 44.045, 44.4233333333333, 
    27.8066666666667, 46.8333333333333, 43.9316666666667, 38.2333333333333, 
    NaN, NaN), sd48 = c(1.01755917108867, 1.42307296603746, 0.193356320472507, 
    2.84609674232389, 1.87677382760949, 0.423922162666686, NA, 
    2.26879483426774, 0.358087512581306, 2.09657259990363, 1.62380622817708, 
    0.851884186181821, 2.59388511696258, NA, NA, 1.6689038318609, 
    2.03437213901488, 0.25646962133295, 1.2880165630405, 1.65534286478663, 
    0.467307892793035, NA, 0.727672087321388, 1.20783552964245, 
    0.755973985443062, 0.670395405712181, 1.0158231473375, 1.33246638481677, 
    NA, NA, 0.849564594365842, 1.99014237346645, 0.590525189979226, 
    0.655926825187079, 2.3131767478225, 0.129871731591854, NA, 
    1.32381897050415, 2.80529796397222, 1.2578831424262, 1.38719020565554, 
    2.9965474577698, 1.29185783531574, NA, NA, 0.862430673542324, 
    1.41170346272391, 4.85495794695141, 1.20460228568049, 1.61017700890306, 
    0.307749898456523, NA, 1.86313445569556, 2.0863045479188, 
    2.90757401740122, 2.07688869867084, 1.8347034274418, 3.43248986402971, 
    NA, NA), n = c(3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 
    3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 
    3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 
    3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 
    3L, 3L, 1L, 1L), max_n = c(3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 3L, 1L, 1L), fac = c(1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), groups = structure(list(
    strain = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L), levels = c("ATCC", "rfs1", "rfs2", "rfs3"
    ), class = "factor"), medium = structure(c(1L, 1L, 1L, 2L, 
    2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 
    2L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L), levels = c("lim", "ynb", 
    "ypg"), class = "factor"), iron = structure(c(1L, 2L, 3L, 
    1L, 2L, 3L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 3L), levels = c("fecl", 
    "feso", "pur"), class = "factor"), .rows = structure(list(
        1:3, 4:6, 7L, 8:10, 11:13, 14L, 15L, 16:18, 19:21, 22L, 
        23:25, 26:28, 29L, 30L, 31:33, 34:36, 37L, 38:40, 41:43, 
        44L, 45L, 46:48, 49:51, 52L, 53:55, 56:58, 59L, 60L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -28L), .drop = TRUE))

The code I used:

library(tidyverse)

rfs_agar_sum <- rfs_agar %>%
  group_by(strain, medium, iron, concentration)%>%
  summarise(
    mean24 = mean(diameter24, na.rm =TRUE),
    sd24=sd(diameter24,na.rm=TRUE),
    mean48 = mean(diameter48, na.rm =TRUE),
    sd48=sd(diameter48,na.rm=TRUE)
    ) %>%
  add_count(medium, iron) %>%
  mutate(max_n = max(n), fac= n/ max_n)%>%
  dput()
       
rfs_sum_plot1 <- ggplot(data= rfs_agar_sum, aes(x=concentration, y=mean24, fill=strain, width=fac))+
  geom_col(stat="identity", color="black", width=0.5, position = position_dodge2(preserve = "single"))+
  geom_errorbar(aes(ymin=mean24-sd24, ymax=mean24+sd24), width=.2,
                position=position_dodge(.6)) +
  facet_wrap(medium ~ iron, scales="free")+
  scale_x_discrete(expand = c(0, 0.5)) + 
  theme_bw()
rfs_sum_plot1 

The plot

benson23
  • 16,369
  • 9
  • 19
  • 38
mr.raccoon
  • 47
  • 7

0 Answers0