如何让地图只显示所需的部分
How to get map to show only desired portion
假设我有这段代码(修改自OpenStreetMaps autoplot error in RStudio Server and Shiny Server in R4.0.0):
library(maps)
library(OpenStreetMap)
library(ggplot2)
library(tidyverse)
mp <- openmap(c(33,-95), c(43,-73),4,'stamen-watercolor')
states_map <- map_data("state")
states_map_merc <- as.data.frame(projectMercator(states_map$lat,states_map$long))
states_map_merc$group <- states_map$group
counties_map <- map_data("county")
counties_map_merc <- as.data.frame(projectMercator(counties_map$lat,counties_map$long))
counties_map_merc$group <- counties_map$group
OpenStreetMap::autoplot.OpenStreetMap(mp,expand=FALSE) +
geom_polygon(data=states_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.5) +
geom_polygon(data=counties_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.1)
我怎样才能只得到它来制作图表的彩色部分?换句话说,只有 openmap
命令中定义的部分。
目前看起来像:
另外,这些警告消息是什么意思?他们有什么可担心的吗?
Warning messages:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded datum World Geodetic System 1984 in Proj4 definition
我从mp对象中获取到bbox信息如下:
> mp[["bbox"]][["p1"]]
[1] -10575352 5311972
> mp[["bbox"]][["p2"]]
[1] -8126323 3895304
然后修改ggplot的limits如下:
OpenStreetMap::autoplot.OpenStreetMap(mp,expand=FALSE) +
geom_polygon(data=states_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.5) +
geom_polygon(data=counties_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.1)+
scale_x_continuous(expand = c(0,0), limits = c(-10575352 ,-8126323))+
scale_y_continuous(expand = c(0,0), limits = c(3895304, 5311972))
最后两行定义了轴的扩展和限制。更多信息here
编辑:
假设我有这段代码(修改自OpenStreetMaps autoplot error in RStudio Server and Shiny Server in R4.0.0):
library(maps)
library(OpenStreetMap)
library(ggplot2)
library(tidyverse)
mp <- openmap(c(33,-95), c(43,-73),4,'stamen-watercolor')
states_map <- map_data("state")
states_map_merc <- as.data.frame(projectMercator(states_map$lat,states_map$long))
states_map_merc$group <- states_map$group
counties_map <- map_data("county")
counties_map_merc <- as.data.frame(projectMercator(counties_map$lat,counties_map$long))
counties_map_merc$group <- counties_map$group
OpenStreetMap::autoplot.OpenStreetMap(mp,expand=FALSE) +
geom_polygon(data=states_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.5) +
geom_polygon(data=counties_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.1)
我怎样才能只得到它来制作图表的彩色部分?换句话说,只有 openmap
命令中定义的部分。
目前看起来像:
另外,这些警告消息是什么意思?他们有什么可担心的吗?
Warning messages:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded datum World Geodetic System 1984 in Proj4 definition
我从mp对象中获取到bbox信息如下:
> mp[["bbox"]][["p1"]]
[1] -10575352 5311972
> mp[["bbox"]][["p2"]]
[1] -8126323 3895304
然后修改ggplot的limits如下:
OpenStreetMap::autoplot.OpenStreetMap(mp,expand=FALSE) +
geom_polygon(data=states_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.5) +
geom_polygon(data=counties_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.1)+
scale_x_continuous(expand = c(0,0), limits = c(-10575352 ,-8126323))+
scale_y_continuous(expand = c(0,0), limits = c(3895304, 5311972))
最后两行定义了轴的扩展和限制。更多信息here
编辑: