简体   繁体   English

在 R 中使用 plot_grid 和 get_legend 的困难

[英]difficulty in using plot_grid and get_legend in R

I am finding difficulty in using plot_grid() and get_legend() in cowplot in R我发现在 R 的 cowplot 中使用plot_grid()get_legend()有困难

I generated six plots using the sample code below.我使用下面的示例代码生成了六个图。 I want to use plot_grid() in cowplot to arrange in 4 rows:我想在 cowplot 中使用plot_grid()来排列 4 行:

  1. row1 - title of plot ("Sample Plot" at the center) row1 - plot 的标题(“Sample Plot”在中心)
  2. row2 - p1[[1]], p1[[2]], p1[[3]] (each with legend at bottom) row2 - p1[[1]], p1[[2]], p1[[3]] (每个底部都有图例)
  3. row3 - p1[[4]], p1[[5]], p2 (no legends, but get_legend() from p2) row3 - p1[[4]], p1[[5]], p2(没有图例,但是来自 p2 的get_legend()
  4. row4 - plot legend extracted from p2 (horizontally aligned), it should appear as shared legend among plots in row 3第 4 行 - 从 p2 提取的 plot 图例(水平对齐),它应该在第 3 行的图中显示为共享图例

relative heights of rows <- c(1, 4, 4, 1)行的相对高度 <- c(1, 4, 4, 1)

Can someone please share the code for the desired plot?有人可以分享所需 plot 的代码吗?

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)

See if this works for you:看看这是否适合你:

# get title as a grob object
grob_title <- get_plot_component(p2 + ggtitle(title) +
                                   theme(plot.title = element_text(hjust = 0.5)), 
                                 "title", 
                                 return_all = TRUE)[[2]]

plot_grid(
  # row 1: center-aligned title
  grob_title,
  
  # row 2: 3 plots from list, each with legends intact
  plot_grid(plotlist = p1[seq(1, 3)], 
            nrow = 1),
  
  # row 3: 2 plots from list + p2, all with legend removed
  plot_grid(p1[[4]] + theme(legend.position = "none"), 
            p1[[5]] + theme(legend.position = "none"), 
            p2 + theme(legend.position = "none"),
            nrow = 1),
  
  # row 4: legend from p2, lengthened (width can be adjusted) & rotated 
  get_legend(p2 + 
               theme(legend.key.width = unit(2, "cm"),
                     legend.position = "bottom")),
  
  ncol = 1, rel_heights = c(1, 4, 4, 1))

结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 cowplot 的 plot_grid 放置图例 - Placement of legend using plot_grid of cowplot 如何使用 R 中的“plot_grid”减少一列图的 plot 边距? - How to reduce plot margins of one column of plots of an arrange of plots using `plot_grid` in R? 如何使用 plot_grid() 将两个图组合在一起在“cowplot”中有一个单一的网格背景? 在 R 语言 - How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language ggplot:使用RColorBrewer软件包填充Boxplots +使用R Cowxlot软件包中的plot_grid()绘制Boxplots - ggplot: Fill Boxplots Using the Package RColorBrewer + Plot Boxplots Using plot_grid() in the Cowplot Package in R R Plot 条在 grid.arrange/plot_grid 中消失 - R Plot Bars are disappearing in grid.arrange/plot_grid 如何使用R中的plot_grid()函数绘制几个seqplots(TraMineR包)? - How to plot several seqplots (TraMineR package) using plot_grid() function in R? 如何在 R 中使用 `ggdraw` 和 `plot_grid()` 在 X 和 Y 轴上创建一个公共标题? - How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R? 将 hist() 和 boxplot() object 转换为 ggplot() object 以使用 R 中的 Cowplot Package 中的 plot_grid() 对齐我的绘图 - Converting a hist() and boxplot() object into a ggplot() object to align my plots using plot_grid() in the Cowplot Package in R 如何使用plot_grid放置没有任何空间的绘图? - How to put plots without any space using plot_grid? Cowplot程序包:在R中使用plot_grid()将许多图安排成一个图后,如何垂直向下调整图例 - Cowplot Package: How to align legends vertically downwards after arranging many plots into one plot using plot_grid() in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM