如何在 ggplot 中绘制边界框?

How can I plot bounding boxes in ggplot?

我有矩形边界框的坐标。例子.

maxlat = 49,minlat = 30,maxlong = -117,minlong = -125

定义边界。

如何在美国地图上放一个对应的方框?

基本上我在找这样的图:

我想放六个这样的盒子放在一张美国地图上。如果我能在地块旁边加上像 1、2、3、4 这样的序列号,那就更好了。但只是把盒子放在那里也很棒。

附录:

美国地图是用这个制作的:

usamap <- map_data("state")
ggplot() + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  xlab('Longitude')+
  ylab('Latitude')+
  coord_map(projection = "mercator")+
  theme_bw()+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#808080"))+
  ggsave("usmap_blank.png",width=10, height=8,dpi=300)

试试这个:

library(maps)
map("state", boundary= TRUE)
rect(ytop = 49, ybottom = 30, xright = -117, xleft = -125, border = "red")

或者,如果您使用 ggplot:

library(ggplot2)
usamap <- map_data("state")
p <- ggplot() + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  xlab('Longitude')+
  ylab('Latitude')+
  coord_map(projection = "mercator")+
  theme_bw()+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#808080"))
p + annotate(geom = "rect", ymax = 49, ymin = 30, xmax = -117, xmin = -125, colour = "red", fill = NA)

您可以使用您想要的注释信息创建一个 data.frame

boxes<-data.frame(maxlat = 49,minlat = 30,maxlong = -117,minlong = -125, id="1")
boxes<-transform(boxes, laby=(maxlat +minlat )/2, labx=(maxlong+minlong )/2)

然后将框和标签添加为单独的图层

usamap <- map_data("state")
ggplot() + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  xlab('Longitude')+
  ylab('Latitude')+
  coord_map(projection = "mercator")+
  geom_rect(data=boxes, aes(xmin=minlong , xmax=maxlong, ymin=minlat, ymax=maxlat ), color="red", fill="transparent") + 
  geom_text(data=boxes, aes(x=labx, y=laby, label=id), color="red") + 
  theme_bw()+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#808080"))

您可以在方框 table.

中为任意数量的方框添加行