Мне трудно использовать plot_grid()
и get_legend()
в коровьем заговоре в R
Я создал шесть графиков, используя приведенный ниже пример кода. Я хочу использовать plot_grid()
в коровьем графике, чтобы расположить его в 4 ряда:
- row1 - название участка (Образец участка в центре)
- row2 - p1 [[1]], p1 [[2]], p1 [[3]] (каждый с легендой внизу)
- row3 - p1 [[4]], p1 [[5]], p2 (без легенд, но
get_legend()
из p2) - row4 - легенда графика, извлеченная из p2 (выровненная по горизонтали), она должна отображаться как общая легенда среди графиков в строке 3
относительная высота строк ‹- c(1, 4, 4, 1)
Кто-нибудь может поделиться кодом желаемого сюжета?
library(tidyverse)
library(cowplot)
# FIRST FIVE PLOT
p1 <- list()
for (iter in 1:5)
{
# generate random points
x <- rnorm(100, mean = 0, sd = 1)
y <- rnorm(100, mean = 0, sd = 1)
# random array of 0 and 1
choice <- sample(c(0,1), replace=TRUE, size=100)
# tibble
tbl <- tibble(x, y, choice)
# plot
p1[[iter]] <- ggplot(data = tbl,
aes(x = x,
y = y,
color = choice)) +
geom_point() +
theme(legend.position = "bottom")
}
# SIXTH PLOT
# generate random points
x <- rnorm(100, mean = 5, sd = 5)
y <- rnorm(100, mean = 5, sd = 5)
# random array of 0 and 1
choice <- sample(c(0,1), replace=TRUE, size=100)
# tibble
tbl <- tibble(x, y, choice)
# plot
p2 <- ggplot(data = tbl,
aes(x = x,
y = y,
color = choice)) +
geom_point() +
theme(legend.position = "right")
title <- "Sample Plot"
# to be edited
plot_grid(p1[[1]], p1[[2]], p1[[3]],
p1[[4]], p1[[5]], p2,
nrow = 2)