r - R中的ggmap在世界地图中不需要的平行线
问题描述
我正在使用 ggmap 绘制世界地图。我的代码如下:
library(maps)
library(ggmap)
library(repr)
library(ggthemes)
library(ggplot2)
options(repr.plot.width=16, repr.plot.height=8)
world_map<-map_data("world")
world_map<-world_map[order(world_map$order),]
ggplot(world_map,aes(x=long,y=lat))+
geom_polygon(aes(group = group, fill = "red"),color='white')+
coord_map("mercator")+
theme_map()
我收到不需要的线路(穿越俄罗斯)。
@mrhellmann 谢谢;它解决了这个问题;(由于某种原因无法回复)
解决方案
您可以使用该sf
包来转换您从 ggplot2 获得的数据。
将数据框转换为sf
对象,然后使用geom_sf
.
library(dplyr)
library(ggplot2)
library(sf)
world_map <- map_data('world')
world_sf <- world_map %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326) %>%
group_by(group) %>%
summarise(geometry = st_combine(geometry)) %>%
st_cast('POLYGON')
ggplot(world_sf) +
geom_sf(fill = 'red')
或者,使用rnaturalearth
包来获得一个干净的sf
对象来使用:
library(rnaturalearth)
ne_world <- ne_countries(returnclass = 'sf')
ggplot(ne_world) +
geom_sf(fill = 'red') +
theme_void()
由reprex 包(v0.3.0)于 2020 年 12 月 29 日创建
推荐阅读
- visual-studio - 我可以强制 Visual Studio 不使用特定的第 3 方库吗?
- python - 为什么 Python 在尝试强制转换时认为我正在创建一个变量?
- java - RestAssured 自定义 TLS 版本
- python - 不使用绝对路径导入 Wheel 文件依赖
- .net - .NET Framework (mscorlib) 中的错误阻止堆栈跟踪行号(在便携式 pdb 中)?
- flutter - Flutter,有人知道如何使用这个 api 更新我的用户资料吗?使用 laravel api 颤动
- c++ - 如何解决由于函数中数组的默认值声明而导致的错误
- azure - 资源组的使用
- python - 如何读取多个 txt 文件 - Python
- dll - .Net Standard 2.0 和 Microsoft.Windows.Compatibility:找不到文件或程序集“System.Drawing.Common,版本 = 4.0.0.1”