本文介绍了使用annotation_custom()在指定坐标处绘制几张图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想将ggplot图放置在地图上的指定位置。我选择了 ggplot2 包,因为我更熟悉它,然后使用 grid 。如果有人会帮助我做一个小例子,说明如何使用 grid 来完成这样的任务,那么我也会对这样的答案感激。 下面是一个简单的例子: #create base plot g 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 geom_point()+ tm p2 geom_point()+ tm #使用annotation_custom()函数 a1 xmin = -104,xmax = -102, ymin = 33,ymax = 35) a2 xmin = -100,xmax = -98 , ymin = 35,ymax = 37) #绘制g + a1 g + a2 g + a1 + a2 但是在 g + a1 + a2 的情况下,我只插入了第一个绘图 p1 的第一张图片。 有什么问题?如何使用 annotation_custom()?绘制两张或更多的图解决方案这是我最近注意到的一个奇怪的错误;对于ggplot2认为类似的一些grobs,位置可以被忽略,并且它们最终叠加: myGrob myGrob2 #这是好的 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 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 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 plotg <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) + geom_blank()# create themetm <- 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 plotp1 <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) + geom_point() + tmp2 <- ggplot(data.frame(x=c(-100,-98), y=c(34,37)), aes(x=x, y=y)) + geom_point() + tm# place them using annotation_custom() functiona1 <- 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)# drawg + a1g + a2g + a1 + a2But 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 fineqplot(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 otherp <- 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 themI have no idea what's causing this, but presumably it's related to your problem. 这篇关于使用annotation_custom()在指定坐标处绘制几张图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 09:07