如何在ggplot中绘制边框?

时间:2022-11-29 20:55:29

I have the coordinates for a rectangular bounding box. Example.

我有一个矩形框的坐标。的例子。

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

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

that defines the bounds.

这定义了边界。

How can I put a box corresponding to this on the US map?

如何在美国地图上放一个对应的盒子?

Basically I am looking for a figure like this: 如何在ggplot中绘制边框?

基本上我要找的是这样的一个数字:

I want to put six such boxes to put on one US Map. If I can put a serial number like 1,2,3,4 next to the plots, that would be even better. But just putting the boxes there would also be great.

我想把六个这样的盒子放在一张美国地图上。如果我可以在图旁边放一个序号,比如1 2 3 4,那就更好了。但是仅仅把盒子放在那里也是很好的。

Addendum:

附录:

The US Map was made using this:

美国地图是这样制作的:

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)

2 个解决方案

#1


3  

You could create a data.frame with the annotation infomation you would like

您可以创建一个带有您想要的注释信息的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)

and then add the boxes and labels as separate layers

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

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"))

如何在ggplot中绘制边框?

You can add rows for as many boxes as you would like in the boxes table.

您可以为box表中所需的数量添加行。

#2


1  

Try this:

试试这个:

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

Or, if you use ggplot:

或者,如果你使用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)

#1


3  

You could create a data.frame with the annotation infomation you would like

您可以创建一个带有您想要的注释信息的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)

and then add the boxes and labels as separate layers

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

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"))

如何在ggplot中绘制边框?

You can add rows for as many boxes as you would like in the boxes table.

您可以为box表中所需的数量添加行。

#2


1  

Try this:

试试这个:

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

Or, if you use ggplot:

或者,如果你使用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)