r - Draw several plots at specified coordinates using annotation_custom() -
i want place ggplot graphs in specified locations on map. chose ggplot2
package because i'm more familiar grid
. if me small example how use grid
such task, appreciate such answer well.
here 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 2 plot should placed on 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 case of g + a1 + a2
obtain first picture first plot p1
inserted. what's wrong? how draw 2 , more plots using annotation_custom()
?
it's weird bug noticed recently; grobs ggplot2 considers similar, position can ignored , end superposed:
mygrob <- rectgrob(gp=gpar(fill="red", alpha=0.5), name="whatever") mygrob2 <- rectgrob(gp=gpar(fill="blue", alpha=0.5)) # fine qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) + annotation_custom(mygrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) + annotation_custom(mygrob2, xmin=8, xmax=12, ymin=3.5, ymax=5.5) # using twice same grob, sit on top of each other p <- qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) + annotation_custom(mygrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) + annotation_custom(mygrob, xmin=8, xmax=12, ymin=3.5, ymax=5.5) g <- ggplotgrob(p) grid.ls(g$grobs[[4]]) # 2 of them
i have no idea what's causing this, presumably it's related problem.
Comments
Post a Comment