在 r 中的同一图表上绘制具有不同 x-axis 百分比比例的 geom_col 和 geom_line

Plot geom_col and geom_line with different x-axis percentage scales on the same graph in r

我创建了以下示例数据:

df <- data.frame(list(Category = c("A", "A", "A", "B", "B", 
        "B", "B", "B", "B", "C", "C", "C", "D", "D", "D","E", 
        "E", "E"), Axis2 = c(0.34, 0.34, 0.34, 0.22, 0.22, 0.22, 
        0.29, 0.29, 0.29, 0.53, 0.53, 0.53, 0.67, 0.67, 0.67, 0.42, 
        0.42, 0.42), Offices = c("Office 1", "Office 2", "Office 3", 
        "Office 1", "Office 2", "Office 3", "Office 1", "Office 2", 
        "Office 3", "Office 1", "Office 2", "Office 3", "Office 1", 
        "Office 2", "Office 3", "Office 1", "Office 2", "Office 3"), 
        Axis1 = c(0.03, 0.02, 0.04, 0.1, 0.08, 0.07, 0.12, 0.211, 
        0.18, 0.05, 0.07, 0.08, 0.01, 0.02, 0.03, 0.07, 0.011, 0.012)))

我想在 y-axis 上绘制 Category 列,Axis1 列作为条形图的 x-axis 和 Axis2 列作为线图的 x-axis。条形图的填充也由 Category 列决定。然后数据由 Offices.

分面

我有以下情节代码:

ggplot(df) + theme_minimal() + guides(fill = FALSE) +
  geom_col(aes(x = Axis1, y = Category, fill = Category)) +  
  geom_line(aes(x = Axis2, y = Category), stat = "identity", group = 1) +
  labs(y = element_blank(), x = "Percentage of Thing 1") +
  facet_wrap(~Offices, ncol = 3) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1),
                     sec.axis = sec_axis(~./max(data$Compliance), 
                                         label=scales::percent)) +
  theme(axis.text.y = element_text(size = 11, color = "black"),
        strip.text = element_text(size = 18, color = "black", 
        face = "bold", margin = margin(c(0.2,0,0.6,0), unit = "in")),
        axis.title.x = element_text(size = 12, color = "black", 
        margin = margin(c(0.2,0,0.1,0), unit = "in")),
        panel.grid.major.x = element_line(color = "gray86"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

我面临 3 个主要问题:

  1. 第二个 x-axis 的位置,我希望它在情节正上方的分面标题下。
  2. 第二个 x-axis 标签未显示 Axis2 列中的数据
  3. 两个图似乎仍在使用主要 x-axis 比例,因为条形图已缩小以适应线图。我希望它们在共享 y 轴但 x-axes 不是的地方彼此独立。

要将条带标签放置在轴上方,请使用 strip.placement= "outside"。要为您的轴获得正确的比例,您还必须缩放要在辅助比例上绘制的数据。

library(ggplot2)

scale <- max(df$Axis1)

ggplot(df) +
  theme_minimal() +
  guides(fill = "none") +
  geom_col(aes(x = Axis1, y = Category, fill = Category)) +
  geom_line(aes(x = Axis2 * scale, y = Category), stat = "identity", group = 1) +
  labs(y = element_blank(), x = "Percentage of Thing 1") +
  facet_wrap(~Offices, ncol = 3) +
  scale_x_continuous(
    labels = scales::percent_format(accuracy = 1),
    sec.axis = sec_axis(~ . / scale, label = scales::percent)
  ) +
  theme(
    axis.text.y = element_text(size = 11, color = "black"),
    strip.text = element_text(
      size = 18, color = "black",
      face = "bold", margin = margin(c(0.2, 0, 0.6, 0), unit = "in")
    ),
    axis.title.x = element_text(
      size = 12, color = "black",
      margin = margin(c(0.2, 0, 0.1, 0), unit = "in")
    ),
    panel.grid.major.x = element_line(color = "gray86"),
    panel.grid.major.y = element_blank(),
    panel.grid.minor = element_blank(),
    strip.placement = "outside"
  )