修复刻面包裹条形图 ggplot2 中的条宽

Fix bar width in facet wrapped barplots ggplot2

这是我的数据示例。我有几个不同的传感器数据 (Var1) 被传感器 (Var2) 分解。这是一个可重现的数据集。

var.sens1 <- structure(list(Var1 = structure(c(9L, 9L, 9L, 9L, 9L, 1L, 2L, 
                                           9L, 9L), 
                                         .Label = c("bathymetry", "current", "deep scattering layer", 
                                                    "frontal data", "imagery", "ocean productivity", 
                                                    "salinity",  "sea surface height", "sea surface temperature",
                                                    "vessel", "wind"), 
                                         class = "factor"), 
                        Var2 = structure(c(1L, 2L, 3L, 4L, 7L, 8L, 8L, 8L, 10L), 
                                         .Label = c("AMSR-E", "AVHRR", "CZCS", "Imager", 
                                                    "IRS-P4 OCM", "MERIS", "MODIS", "not specified", 
                                                    "OCI", "SeaWiFS", 
                                                    "SeaWinds"), class = "factor"), 
                        Freq = c(4L, 37L, 1L, 3L, 20L, 4L, 20L, 26L, 2L)), 
                   row.names = c(9L, 20L, 31L, 42L, 75L, 78L,79L, 86L, 108L), 
                   class = "data.frame")

然后我使用此代码对我的数据进行分面包装(而不是堆叠条形图)。

vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) + 
  geom_bar(stat="identity")

vp + facet_wrap( ~ Var1, ncol=3, scales="free_y") +
  coord_flip() +
  theme_light()+
  theme(legend.position="none") +
  xlab("Sensor") + ylab("Frequency")

我使用 scales_y=free 删除图表中不需要的 space。得到如下图。

但是,条宽大小不同。有谁知道如何在刻面环绕时固定条形宽度?修复 geom_bar 中的 barwidth 似乎不起作用。将 geom_col 与 position_dodge2 一起使用也不会按照建议 进行保存。请参阅下面的代码和图片。

vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) + 
  geom_col(position = "dodge")

vp + 
facet_grid(~Var1, scales = "free_y")+
coord_flip() +
theme_light()+
theme(legend.position="none") +
xlab("Sensor") + ylab("Frequency")

我更喜欢在小平面网格上使用 facet_wrap,所以我没有一长串的图(我有多个变量需要在我的数据集中绘制,而不仅仅是显示的三个)。

使用geom_col (position = position_dodge2(preserve = "single"))

vp <- ggplot(var.sens1, aes(x=Var2, y=Freq, fill=Var1, label=Freq)) + 
  geom_col(position = position_dodge2(preserve = "single"))

vp + 
  facet_grid(~Var1, scales = "free_y")+
  coord_flip() +
  theme_light()+
  theme(legend.position="none") +
  xlab("Sensor") + ylab("Frequency")

enter image description here