【问题标题】:How to get map to show only desired portion如何让地图只显示所需的部分
【发布时间】:2021-08-10 23:34:45
【问题描述】:

说我有这段代码(修改自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

【问题讨论】:

  • 我希望你能得到其他的意见......但一个简单的想法是通过你的绑定坐标 c(33, -95) 和 c(43, - 73)。例如,将subset(states_map, lat &gt; 33 &amp; lat &lt; 43 &amp; long &gt; -95 &amp; long &lt; -73)projectMercator 参数一起使用,而不是完整的states_map 数据,并对counties_map 执行相同的操作。然后,仅包含您感兴趣区域内的经度和纬度(或您决定的任何限制)...
  • 此外,this vignette 可能会提供一些关于警告的想法。这似乎与 proj 库的更改有关,另请参阅 this。 OpenStreetMap 中的projectMercator 函数正在使用proj4string 并可能导致警告
  • 这很有帮助,本。谢谢。

标签: r ggplot2 maps openstreetmap


【解决方案1】:

我从mp对象中获取bbox信息如下:

> mp[["bbox"]][["p1"]]
[1] -10575352   5311972
> mp[["bbox"]][["p2"]]
[1] -8126323  3895304 

然后修改ggplot的限制如下:

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

编辑:

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多