在ggplot绘图中包含多个grobs对象。

3

如何在ggplot plot图表中包含多个grobs对象,而不是每次都使用annotation_custom

这是我的代码:

df <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = 1 , y = 1)
g <- ggplotGrob(ggplot(df2, aes(x, y)) +
                  geom_point() +
                  theme(plot.background = element_rect(colour = "black")))

base <- ggplot(df, aes(x, y)) +
  geom_blank() +
  theme_bw()
base +
  annotation_custom(grob = g, xmin = 3, xmax = 2, ymin = 8, ymax = 10)+

  annotation_custom(grob = g, xmin = 1.5, xmax = 2.5, ymin = 2.8, ymax = 3)+

  annotation_custom(grob = g, xmin = 1.7, xmax = 2.7, ymin = 3.8, ymax = 5)+

  annotation_custom(grob = g, xmin = 5, xmax = 6, ymin = 7, ymax = 8.5)

我的实际问题比这个例子更严重。

有没有一种方法可以把所有这些“grobs”例如放在一个列表中,并只选择我想要定位它们的坐标。还是我需要一直使用annotation_custom

求助。


你能使用 dput(df) 为我们提供 df 吗? - neuron
2个回答

4

一种选项是将坐标和图形对象放在一个tibble中,然后使用purrr::pmap来循环遍历tibble的行以将图形对象添加到您的绘图中:

library(ggplot2)
library(tibble)
library(purrr)

d_grob <- tibble(
  xmin = c(3, 1.5, 1.7, 5),
  xmax = c(2, 2.5, 2.7, 6),
  ymin = c(8, 2.8, 3.8, 7),
  ymax = c(10, 3, 5, 8.5),
  grob = list(g, g, g, g)
)

base +
  purrr::pmap(d_grob, function(grob, xmin, xmax, ymin, ymax) annotation_custom(grob = grob, xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax))


2

您可以使用列表将ggplot图层添加到绘图中。一种方法是创建一个函数,该函数接受您使用ggplotGrob()创建的gross的x和y坐标列表,然后创建annotation_custom对象并将其添加到您最后返回的列表中。

df <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = 1 , y = 1)
g <- ggplotGrob(ggplot(df2, aes(x, y)) +
                  geom_point() +
                  theme(plot.background = element_rect(colour = "black")))

add_rects <- function(x_coords, y_coords) {
  # First test that the x and y lists are the same length. 
  # If they are not throw an error.
  if (length(x_coords) != length(y_coords)) {
    stop("The x and y coordinates must be the same length")
  }
  
  grob_list <- list()
  
  for (i in 1:length(x_coords)) {
    # Check to see that each element in the list has exactly 2 coordinates
    if (length(x_coords[[i]]) != length(y_coords[[i]] & length(x_coords[[i]]) != 2)) {
      
      stop("Each x and y coordinate must be a vector of length 2 specifying
           the min and max positioning.")
    }
    # Get the min and max values for x and y coordinates
    xmin <- x_coords[[i]][1]
    ymin <- y_coords[[i]][1]
    xmax <- x_coords[[i]][2]
    ymax <- y_coords[[i]][2]
    
    # Create the annotation
    grob <- annotation_custom(grob = g, 
                              xmin = xmin, 
                              xmax = xmax,
                              ymin = ymin,
                              ymax = ymax)
    # Append it to the list that you will return after the loop
    grob_list[[i]] <- grob
  }
  
  return(grob_list)
}


base <- ggplot(df, aes(x, y)) +
  geom_blank() +
  theme_bw()

x_coords <- list(
  c(3, 2),
  c(1.5, 2.5),
  c(1.7, 2.7),
  c(5, 6)
)

y_coords <- list(
  c(8, 10),
  c(2.8, 3),
  c(3.8, 5),
  c(7, 8.5)
)

base +
  add_rects(x_coords = x_coords, y_coords = y_coords)

以下是您的代码生成的图表(如下所示),与我从您的代码中得到的图表完全相同。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接