【问题标题】:Share x-axis and y-axis label on the same combo chart在同一组合图表上共享 x 轴和 y 轴标签
【发布时间】:2022-11-22 04:50:21
【问题描述】:

我需要在同一个组合图表上共享 x 轴和 y 轴标签,使用 egg::ggarrange,

我试过了:

library(ggplot2)
df <- data.frame(
  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4
)

p1 <- ggplot(df, aes(x, y1)) + geom_point() + theme(axis.title.x = element_blank(),
                                                    axis.title.y = element_blank())
p2 <- ggplot(df, aes(x, y2)) + geom_point() + theme(axis.title.x = element_blank(),
                                                    axis.title.y = element_blank())
p3 <- ggplot(df, aes(x, y3)) + geom_point() + theme(axis.title.x = element_blank(),
                                                    axis.title.y = element_blank())
p4 <- ggplot(df, aes(x, y4)) + geom_point() + theme(axis.title.x = element_blank(),
                                                    axis.title.y = element_blank())

# Create a simple grid
p <- plot_grid(p1, p2, p3, p4, align = 'hv')

p + draw_figure_label(label = "Figure 1")




p <- Map(function(a, b) {
  a + 
    labs(caption = b) + 
    theme(plot.caption.position = 'panel',
          plot.caption = element_text(hjust = 0.5, size = 12, face = 2,
                                      margin = margin(10, 0, 30, 0)))
}, 
a = list(p1, p2, p3, p4),
b = list('(a) Figure 1', '(b) Figure 2', '(c) Figure 3', '(d) Figure 4'))


graph <- egg::ggarrange(p[[1]], p[[2]], p[[3]], p[[4]])


annotate_figure(graph, left = textGrob("axis-y", rot = 90, vjust = 1, gp = gpar(cex = 0.8)),
                bottom = textGrob("axis-x", gp = gpar(cex = 0.8)))

但是,x 轴和 y 轴标签出现在图窗标签之后。而且,他们离得很远。

【问题讨论】:

    标签: r ggplot2 egg


    【解决方案1】:

    不能 100% 确定您想要的最终结果。但也许这就是您正在寻找的。

    恕我直言,棘手的部分是切换子图标题和 x 轴标题的位置。在尝试了一些分面之后,我唯一能想到的方法是将图和子图标题创建为单独的图,我使用 patchwork 将它们粘合在一起。此外,我创建了第五个图来创建共享的 y 轴标题。我方法的第一步是将数据重塑为长格式。之后我使用一些自定义函数来创建绘图:

    library(ggplot2)
    library(patchwork)
    
    df <- data.frame(
      x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4
    )
    
    df_long <- df |>
      tidyr::pivot_longer(-x, names_to = "y")
    
    plot_fun <- function(which, x = "") {
      ggplot(subset(df_long, y %in% which), aes(x, value)) +
        geom_point() +
        facet_wrap(~y, scales = "free") +
        theme(
          strip.background.x = element_blank(),
          strip.text.x = element_blank(),
        ) +
        labs(x = x, y = NULL)
    }
    
    plot_tag <- function(which) {
      ggplot() +
        facet_wrap(~as.character(which),
                   strip.position = "bottom") +
        theme_void() +
        theme(
          strip.placement = "outside",
          strip.background.x = element_blank(),
          strip.text.x = element_text(
            hjust = 0.5, size = 12, face = 2,
            margin = margin(0, 0, 11, 0),
            
          ),
          strip.clip = "off"
        )
    }
    
    plot_y <- function(y) {
      ggplot() +
        theme_void() +
        labs(y = y) +
        theme(axis.title.y = element_text(angle = 90))
    }
    
    p1 <- plot_fun(c("y1", "y2"))
    p2 <- plot_tag(c("(a) Figure 1", "(b) Figure 2"))
    p3 <- plot_fun(c("y3", "y4"), "axis-x")
    p4 <- plot_tag(c("(c) Figure 3", "(d) Figure 4"))
    p5 <- plot_y("axis-y")
    
    
    design <- "
    EA
    EB
    EC
    ED
    "
    wrap_plots(list(p1, p2, p3, p4, p5))+ 
      plot_layout(heights = c(40, 1, 40, 1), widths = c(1, 80), design = design)
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      相关资源
      最近更新 更多