gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:15:30 26 4
gpt4 key购买 nike

我正在尝试在 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
创建于 2020-08-04 由 reprex package (v0.3.0)

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com