多面和分组 ggplot 条形图中的标准列宽

Standard column width in facetted and grouped ggplot bar plot

我使用 ggplot 和分组数据制作了一个条形图,并使用 facet_grid 进行了分面。列宽不一致,所以我想让它们都一样。我已经 这可以用 preserve="single 完成,但它似乎搞乱了位置躲避。知道如何防止这种情况发生吗??

这里是一小部分数据:

data <- structure(list(grp2 = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
7L, 7L, 7L, 7L, 7L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 
7L), .Label = c("CSF1", "CSF2", "PC", "NC", "GPC", "GNC", "standard"
), class = "factor"), label2 = structure(c(7L, 8L, 9L, 7L, 8L, 
9L, 7L, 15L, 15L, 15L, 15L, 15L, 7L, 8L, 9L, 7L, 8L, 9L, 7L, 
15L, 15L, 15L, 15L, 15L), .Label = c("CSF1_raw", "CSF1_supernatant", 
"CSF1_pellet", "CSF2_raw", "CSF2_supernatant", "CSF2_pellet", 
"PC_raw", "PC_supernatant", "PC_pellet", "NC_raw", "NC_supernatant", 
"NC_pellet", "GPC", "GNC", "standard", "NC"), class = "factor"), 
    mda_label = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L
    ), .Label = c("none", "mda_20", "mda_200"), class = "factor"), 
    conc = c(`7` = 0, `8` = 0, `9` = 0.324886127298521, `55` = 4.14765656994934, 
    `56` = 1.16840050032707, `57` = 8.33529714053568, `76` = 10.6220645144775, 
    `77` = 48.9241552191721, `78` = 4.51513315624087, `79` = 1.03887911533275, 
    `80` = 0.0445944796011582, `81` = 0.00484116548901831, `89` = 0, 
    `90` = 0, `91` = 0.322922569348207, `137` = 6.38488684568018, 
    `138` = 1.68909814271646, `139` = 7.61828609738757, `158` = 15.3082130743032, 
    `159` = 41.3127531345335, `160` = 4.64193087683391, `161` = 0.411672491030815, 
    `162` = 0.0568193835425769, `163` = 0.00439419098560105)), row.names = c(NA, 
-24L), class = c("tbl_df", "tbl", "data.frame"))

这是初始情节:

ggplot(data, aes(x=label2, y=conc, colour=mda_label, fill=mda_label)) +
  facet_grid(. ~ grp2, scales="free_x", space="free") +
  stat_summary(fun = mean, geom = "bar", position = position_dodge()) + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
               position = position_dodge(width=0.9)) +
  geom_point(position = position_dodge(width=0.9), pch=21, colour="black") +
  scale_y_continuous(trans='pseudo_log',
                     labels = scales::number_format(accuracy=0.01)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

但是当我尝试用 preserve="single" 标准化列宽时,它变得一团糟:

ggplot(data, aes(x=label2, y=conc, colour=mda_label, fill=mda_label)) +
  facet_grid(. ~ grp2, scales="free_x", space="free") +
  stat_summary(fun = mean, geom = "bar", position = position_dodge(preserve="single")) + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
               position = position_dodge(width=0.9, preserve="single")) +
  geom_point(position = position_dodge(width=0.9, preserve="single"), pch=21, colour="black") +
  scale_y_continuous(trans='pseudo_log',
                     labels = scales::number_format(accuracy=0.01)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

由于您使用的数据为 0 值,因此您可以为 grp2/label2 标准类别中的其他 'mda_label' 设置 0 值。

data <- rbind(data, data.frame(grp2 = c("standard", "standard"),
                           label2 = c("standard", "standard"),
                           mda_label = c("mda_20", "mda_200"),
                           conc = c(0, 0)))

而且你从来没有真正制作过条形图

data %>%
  ggplot(aes(label2, conc, fill = mda_label)) +
  geom_col(position = position_dodge(width = 1)) +
  facet_grid(. ~ grp2, scales = "free", space = "free")