How can I include multiple grobs object in ggplot plot chart without using annotation_custom
all the time?
This is my code:
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)
My real problem is bigger, this is just an example.
Is there a way to put all this grobs for example, in a list, and just choose the coordinates that I want to positioning them. Or I need to use annotation_custom
all the time?
Any help
One option would be to put the coordinates and grobs in a tibble
and use purrr::pmap
to loop over the rows of the tibble to add the grobs to your plot:
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))
You can add ggplot
layers to a plot using a list. One way to approach this is to create a function that accepts a list of x and y coordinates for the gross that you created with ggplotGrob()
, then create the annotation_custom
object and add it to a list that you return at the end.
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)
Which gives a plot (below) that look identical to what I got from your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With