使用annotation_custom()在指定坐标处绘制几张图 [英] Draw several plots at specified coordinates using annotation_custom()

查看:183
本文介绍了使用annotation_custom()在指定坐标处绘制几张图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将ggplot图放置在地图上的指定位置。我选择了 ggplot2 包,因为我更熟悉它,然后使用 grid 。如果有人会帮助我做一个小例子,说明如何使用 grid 来完成这样的任务,那么我也会对这样的答案感激。



下面是一个简单的例子:

 #create base plot 
g < - ggplot(data .frame(x = c(-104,-94),y = c(33,38)),aes(x = x,y = y))+
geom_blank()

)创建主题
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 =gray,fill = NA),
title = element_text(size = 5))

#创建两个应放置在底图上的图形
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

#使用annotation_custom()函数
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)

#绘制
g + a1
g + a2
g + a1 + a2



但是在 g + a1 + a2 的情况下,我只插入了第一个绘图 p1 的第一张图片。
有什么问题?如何使用 annotation_custom()?绘制两张或更多的图

解决方案

这是我最近注意到的一个奇怪的错误;对于ggplot2认为类似的一些grobs,位置可以被忽略,并且它们最终叠加:

  myGrob < -  rectGrob(gp = gpar(fill =red,alpha = 0.5),name =whatever)
myGrob2 < - rectGrob(gp = gpar(fill =blue,alpha = 0.5))

#这是好的
qplot(1:10,1:10)+ theme(plot.margin = unit(c(0,3,0,0),cm))+
annotation_custom(myGrob2,xmin = 8,xmax = 12,ymin = 3.5,ymax = 5.5)
annotation_custom(myGrob,xmin = 5,xmax = 6,ymin = 3.5,ymax = 5.5)+

#使用同一个grob的两倍,他们只是坐在彼此之上
p < - qplot(1:10,1:10)+主题(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]])#其中两个

我不知道是什么导致了这种情况,但推测它与您的问题有关。


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()?

解决方案

It's a weird bug that I noticed recently; for some grobs that ggplot2 considers similar, the position can be ignored and they end up superposed:

myGrob <- rectGrob(gp=gpar(fill="red", alpha=0.5), name="whatever")
myGrob2 <- rectGrob(gp=gpar(fill="blue", alpha=0.5))

# this is 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 the same grob, they just 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]]) # two of them

I have no idea what's causing this, but presumably it's related to your problem.

这篇关于使用annotation_custom()在指定坐标处绘制几张图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆