首页 > 解决方案 > 使用共享 y 轴在 1x4 网格中显示 R 图

问题描述

我正在尝试在 1x4 网格中显示一些图形,但我希望所有图形都具有相同的 x 和 y 轴。

time maxhgs.sleep_LIPA maxhgs.sed_LIPA maxhgs.stand_LIPA maxhgs.MVPA_LIPA maxhgs.LIPA_MVPA
1    5        0.08289621      0.03241295         0.1129983      0.112998341      -0.01928050
2   10        0.16289049      0.06139545         0.2236818     -0.006728721      -0.04950022
3   15        0.24025861      0.08721203         0.3323473     -0.047756360      -0.08927656
4   20        0.31524160      0.11009218         0.4392581     -0.144261526      -0.13791276
5   25        0.38805152      0.13023596         0.5446498     -0.424789999      -0.19517306
6   30        0.41660977      0.13756729         0.5864293     -0.934884300      -0.26117695

这是我正在使用的数据。

library(ggplot2)
library(egg)

maxhgs.a <- ggplot(maxhgs.df, aes(time, maxhgs.sleep_LIPA)) + geom_point()+geom_line()
maxhgs.a <- maxhgs.a + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
  ggtitle("Sleep to LIPA")

maxhgs.b <- ggplot(maxhgs.df, aes(time, maxhgs.sed_LIPA)) + geom_point()+geom_line()
maxhgs.b <- maxhgs.b + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
  ggtitle("Sedentary to LIPA")

maxhgs.c <- ggplot(maxhgs.df, aes(time, maxhgs.stand_LIPA)) + geom_point()+geom_line()
maxhgs.c <- maxhgs.c + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
  ggtitle("Standing to LIPA")

maxhgs.d <- ggplot(maxhgs.df, aes(time, maxhgs.MVPA_LIPA)) + geom_point()+geom_line()
maxhgs.d <- maxhgs.d + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0.5,-1, by=-0.1))+
  ggtitle("MVPA to LIPA")


ggarrange(maxhgs.a, 
          maxhgs.b + 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ), 
          maxhgs.c + 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ),
          maxhgs.d +
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ), 
          nrow = 1)

这是我迄今为止所尝试的。这实际上是“有效的”,因为所有图表都具有相同的 y 轴,但 y 轴实际上并不能反映图表上应该显示的内容。正如您在图中看到的,y 轴从 0.1 到 0.4,但 maxhgs.d 图应该从 0.1 扩展到 -0.9。

在此处输入图像描述

任何意见或建议将不胜感激!

标签: rggplot2

解决方案


您可以通过重塑数据和使用分面来简化此操作。这样,您只需要定义一个图。这需要您pivot_longer将因子级别更改为每个方面所需的名称,但是一旦完成,绘图本身就很简单:

library(ggplot2)
library(dplyr)

# Define the label names for the facets first
labs <- c("LIPA to MVPA", "MVPA to LIPA", "Sedentary to LIPA",
          "Sleep to LIPA", "Standing to LIPA")

gg <- maxhgs.df %>% 
  tidyr::pivot_longer(cols = -1) %>% 
  mutate(plot = factor(`levels<-`(factor(name), labs), labs[c(4, 3, 5, 2, 1)])) %>%
  ggplot(aes(x = time, y = value)) + 
  geom_line() +
  geom_point() +
  scale_x_continuous(name = "Time Reallocated") +
  scale_y_continuous(name = "Change in maxhgs") +
  theme(strip.background = element_blank(),
        strip.text = element_text(size = 13)) 

现在我们可以选择使用固定的 y 轴进行绘图:

gg + facet_grid(.~plot, scale = "fixed")

在此处输入图像描述

或使用灵活的 y 轴:

gg + facet_wrap(.~plot, scale = "free_y", ncol = 5)

在此处输入图像描述

reprex 包(v0.3.0)于 2020 年 8 月 4 日创建


推荐阅读