Настройка ширины и высоты одной панели в многопанельном графике в cowplot :: plot_grid - PullRequest
1 голос
/ 27 марта 2020

Я делаю многопанельный график, используя пакеты ggplot2 и cowplot, но мне нужно изменить высоту одного графика. Проще всего это показать на примере

library(ggplot2)
library(cowplot)

p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.x = element_blank(),
                   axis.title.x = element_blank(),
                   legend.position = "none")
p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.x = element_blank(),
                   axis.title.x = element_blank(),
                   axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")
p3 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")
p4 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() + 
             theme(legend.position = "none")
p5 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")

pL <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + geom_point()
l <- get_legend(pL)

# simple grid
plot_grid(p1, p2, p3, p4, p5, l, ncol = 3)

enter image description here

Как вы можете видеть, ось y в крайней правой верхней панели была уменьшена по сравнению с две другие панели в той же строке путем включения заголовка оси X.

Итак как установить относительную высоту и ширину этой отдельной панели, чтобы ось Y совпала с осью Y панелей в верхнем ряду?

Невозможно установить отдельные панели, используя аргументы rel_heights = и rel_widths() =, и когда я попытался добавить аргументы axis = "tblr" и align = "hv", я получил сообщение об ошибке

Error in `[.unit.list`(sizes[[x]], list_indices[[x]]) : 
  index out of bounds (unit list subsetting) 

Ответы [ 2 ]

2 голосов
/ 27 марта 2020

@ Ответы Тунга были великолепны, но я также решил, как это сделать в коровьем заговоре. Вы просто создаете две отдельные панели, по одной для каждой строки, затем вы можете использовать аргументы align= и axis=, чтобы выровнять ось Y. Мы выравниваем ось Y по вертикали на основе горизонтальных опорных линий, поэтому мы указываем align = "h".

top_row <- plot_grid(p1, p2, p3, align = "h", axis = "l", ncol = 3)

bottom_row <- plot_grid(p4, p5, l, align = "h", axis = "l", ncol = 3)

plot_grid(top_row, bottom_row, ncol = 1)

enter image description here

2 голосов
/ 27 марта 2020

patchwork пакет может сделать работу. Пакеты egg или multipanelfigure тоже могут работать .

# https://github.com/thomasp85/patchwork
# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
#> 
#> Attaching package: 'patchwork'
#> The following object is masked from 'package:cowplot':
#> 
#>     align_plots

layout <- "
ABC
DEF
"
p1 + p2 + p3 + p4 + p5 + l +
  plot_layout(design = layout)

(p1 + p2 + p3)/(p4 + p5 + l) +
  plot_layout(nrow = 2) +
  plot_annotation(title = "Title",
                  subtitle = "Subtitle",
                  tag_levels = 'i',
                  tag_suffix = ')')

Создано в 2020-03-26 с помощью пакета prepx (v0.3.0)

...