R 使用注释_custom()在指定坐标处绘制多个绘图
我想在地图上的指定位置放置ggplot图。我选择了R 使用注释_custom()在指定坐标处绘制多个绘图,r,ggplot2,R,Ggplot2,我想在地图上的指定位置放置ggplot图。我选择了ggplot2package,因为我对它比对grid更熟悉。如果有人能帮我举一个小例子,告诉我如何使用grid来完成这样的任务,我也会非常感谢你的回答 下面是一个简单的例子: # 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(
ggplot2
package,因为我对它比对grid
更熟悉。如果有人能帮我举一个小例子,告诉我如何使用grid
来完成这样的任务,我也会非常感谢你的回答
下面是一个简单的例子:
# 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
#创建基准图
这是我最近注意到的一个奇怪的错误;对于ggplot2认为类似的一些Grob,可以忽略该位置,并最终叠加:
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
myGrob解决方法是将grob包装在gTree
,g1=grobTree(ggplotGrob(p1));g2=grobTree(ggplotGrob(p2))
并在注释中使用这些grob\u custom
。别问我为什么!有关此问题的讨论,请参阅。ggplot2开发已经暂停了一段时间,所以可能没有对这个问题的快速响应。这个问题看起来与#817不同,但可能相关。甚至可能是相同的底层bug。无论如何,我建议你把它作为一个单独的问题来报告。