Функция «ggarrange» в R используется для размещения нескольких графиков ggplot2 на одной странице. Он позволяет настраивать макет и внешний вид графиков. Вот несколько методов, которые можно использовать с примерами кода:
Метод 1: базовое расположение
library(ggplot2)
library(ggpubr)
# Create some example plots
plot1 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()
plot2 <- ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()
# Arrange the plots
ggarrange(plot1, plot2, nrow = 1, ncol = 2)
Метод 2: индивидуальное расположение
library(ggplot2)
library(ggpubr)
# Create some example plots
plot1 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()
plot2 <- ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()
plot3 <- ggplot(mtcars, aes(x = mpg, y = qsec)) + geom_point()
# Arrange the plots with customized layout
ggarrange(plot1, plot2, plot3, nrow = 2, ncol = 2,
widths = c(3, 1), heights = c(1, 2))
Метод 3: групповое расположение
library(ggplot2)
library(ggpubr)
# Create some example plots
plot1 <- ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point()
plot2 <- ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()
plot3 <- ggplot(mtcars, aes(x = mpg, y = qsec)) + geom_point()
plot4 <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
# Arrange the plots in groups
group1 <- ggarrange(plot1, plot2, nrow = 1, ncol = 2)
group2 <- ggarrange(plot3, plot4, nrow = 1, ncol = 2)
ggarrange(group1, group2, nrow = 2, ncol = 1, heights = c(2, 1))
Это всего лишь несколько примеров того, как можно использовать функцию «ggarrange» в R для упорядочивания нескольких графиков ggplot2. Вы можете дополнительно настроить расположение, регулируя такие параметры, как nrow
, ncol
, widths
и heights
для достижения желаемого результата. макет.