r - 使用共享 y 轴在 1x4 网格中显示 R 图

标签 r ggplot2

我试图在 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 轴实际上并不反射(reflect)图表上应有的内容。正如您在图表中看到的,y 轴从 0.1 到 0.4,但 maxhgs.d 图表应从 0.1 延伸到 -0.9。

enter image description here

如有任何意见或建议,我们将不胜感激!

最佳答案

您可以通过 reshape 数据和使用分面来使这变得更加容易。这样,您只需要定义一个图。这需要您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")

enter image description here

或者使用灵活的 y 轴:

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

enter image description here

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

关于r - 使用共享 y 轴在 1x4 网格中显示 R 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63243293/

相关文章:

R 切掉低频箱

r - 如何使用任何变换来缩放/变换 graphics::plot() 轴,而不仅仅是对数(对于威 bool 图)?

r - system.time 的多功能测试器替代品

r - R 中 plot3D::persp3D 中的纵横比

r - 带值的热图(ggplot2)

r - ggplot2 条形图将标签从秒转换为分钟 :sec

r - 保存 "summary(lm)"的结果以在 Power Bi 中使用

r - 带有 log1p 转换的annotation_logticks

c++ - 是否存在针对 C/C++ 的 R 编译器?

如果使用source()运行,则R封装晶格将不会绘制