I want to place ggplot graphs in specified locations on the map. I chose ggplot2
package because I'm more familiar with it then with grid
. If someone will help me with a small example how to use grid
for such a task, I will appreciate for such an answer as well.
Here is a simple example:
# create base plot
g <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) +
geom_blank()
# create theme
tm <- theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank(),
panel.border = element_rect(color="grey", fill=NA),
title = element_text(size=5))
# create two plot which should be placed on the base plot
p1 <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) +
geom_point() + tm
p2 <- ggplot(data.frame(x=c(-100,-98), y=c(34,37)), aes(x=x, y=y)) +
geom_point() + tm
# place them using annotation_custom() function
a1 <- annotation_custom(grob = ggplotGrob(p1),
xmin = -104, xmax = -102,
ymin = 33, ymax = 35)
a2 <- annotation_custom(grob = ggplotGrob(p2),
xmin = -100, xmax = -98,
ymin = 35, ymax = 37)
# draw
g + a1
g + a2
g + a1 + a2
But in the case of g + a1 + a2
I obtain the first picture with only the first plot p1
inserted.
What's wrong? How to draw two and more plots using annotation_custom()
?