如何将条形图居中放置在一个小平面中并删除小平面中的 x 轴标签?

How can I center the bar graphs in a facet wrap and remove the x axis labels in the facets?

我正在寻求有关 ggplot facetwraps 的帮助以生成一个图形,其中条形图在每个小平面内居中,并且小平面的 x 轴上没有标签(因为它们已在上面标记)。

您将如何实现这一目标?

首先,生成数据框

act <- c("Wildlife viewing", "Wildlife viewing", "Wildlife viewing", 
         "Birdwatching", "Birdwatching", "Birdwatching", 
         "Wildlife photography", "Wildlife photography", "Wildlife photography", 
         "Deer hunting", "Deer hunting", "Deer hunting", 
         "Trapping", "Trapping", "Trapping", 
         "Fishing", "Fishing", "Fishing")

ppart <- c(65.87091, 60.64611, 66.12500,
           37.66578, 35.97651, 41.75000,
           29.00088, 26.28488, 30.50000, 
           53.40407, 28.92805, 42.37500, 
           9.018568, 3.524229, 5.250000, 
           63.21839, 44.19971, 57.62500)

Region <- c("UP", "SL", "NL", 
            "UP", "SL", "NL", 
            "UP", "SL", "NL",
            "UP", "SL", "NL",
            "UP", "SL", "NL",
            "UP", "SL", "NL")

df <- data.frame(act, ppart, Region) # create a df with % participation in various activities

然后,创建图形。

ggplot(df, aes(x = act, y = ppart, fill = Region)) + 
    geom_bar(position = "dodge", stat = "identity") + 
    facet_wrap(~act) +
  scale_fill_grey(start = 0.8, end = 0.2) +
    ylab("Percent participation in activity") + 
  xlab("") + 
    theme_bw()

如您所见,当我使用上面的代码生成图形时,有些东西有点模糊,并且间距和标签存在明显的问题。我该如何解决这些问题?

具体而言,将条形图在每个方面居中并删除方面内的 x 轴标签。请指教!谢谢。

这就是你想要的吗?

ggplot(df, aes(x = 0, y = ppart, fill = Region)) + 
  geom_bar(position = "dodge", stat = "identity") + 
  facet_wrap(~act) +
  scale_fill_grey(start = 0.8, end = 0.2) +
  ylab("Percent participation in activity") + 
  xlab("") + 
  theme_bw() +
  theme(
    axis.text.x = element_blank()
  )

我认为规范的方法是在 facet 中使用 scales = "free_x",并使用 breaks=NULL.

显式删除 x 轴刻度
ggplot(df, aes(x = act, y = ppart, fill = Region)) + 
    geom_bar(position = "dodge", stat = "identity") + 
    facet_wrap(~act, scales = "free_x") +   # changed
  scale_fill_grey(start = 0.8, end = 0.2) +
    ylab("Percent participation in activity") + 
  xlab("") + 
    theme_bw() +
  scale_x_discrete(breaks = NULL)           # new