R中是否存在从-360到360度的世界国家多边形?
Is there a polygon of world countries from -360 to 360 degrees in R?
我有点困惑如何绘制不适应 ggplot2 包提供的 map_data 的世界地图,即“world”(从 -180 到 180 度)和“world2” (从 0 到 360 度)。例如,如果我想从 100°E 到 20°E 绘图,ggplot2 提供的多边形是没有用的。这是示例的代码:
library(ggplot2)
map1 <- map_data('world')
map2 <- map_data('world2')
ggplot() +
theme_bw() +
geom_polygon(data = map1,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_fixed(1.4, xlim = c(100,380))
ggplot() +
theme_bw() +
geom_polygon(data = map2,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_fixed(1.4, xlim = c(100,380))
那么,世界地图上是否有像示例中的那些多边形但范围更大的多边形?
与其查找这些多边形,不如使用 coord_quickmap() 或 coord_sf() 裁剪经度边界。请注意 coord_map() 将错误地对多边形的点进行分组,除非在 ggplot() 调用之前被剪裁。
library(tidyverse)
ggplot() +
theme_bw() +
geom_polygon(data = map2,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_sf(xlim = c(20,100))
#coord_quickmap(xlim = c(100,20)) is also an alternative to coord_sf()
这很草率,但为什么不呢?您可以显示一个重复的世界,这样每个区域都可以显示出来而不会越过边缘。
library(dplyr)
map1 <- map_data('world')
map2 <- map_data('world') %>% mutate(long = long+360)
map3 <- map_data('world') %>% mutate(long = long+720)
ggplot() +
theme_bw() +
geom_polygon(data = map1,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
geom_polygon(data = map2,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
geom_polygon(data = map3,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_fixed(1.4, xlim = c(100,480))
“world”和“world2”是在“maps”包中实现的世界地图的两个版本。但是该包实际上允许您定义具有任何边界的世界地图,例如c(20,380) 或 c(100,460).
最简单的方法就是使用“wrap”选项(在 maps::map 的手册页中有描述)。此选项传递给 maps::map()(因此请查看地图包以获取完整帮助页面)
map3 <- map_data('world', wrap=c(20,380))
这应该会为您提供一张适合您选择的任何子午线的地图。请注意,在这种情况下,“wrap”必须是一个向量,并且左右边界必须正好相隔 360。
我有点困惑如何绘制不适应 ggplot2 包提供的 map_data 的世界地图,即“world”(从 -180 到 180 度)和“world2” (从 0 到 360 度)。例如,如果我想从 100°E 到 20°E 绘图,ggplot2 提供的多边形是没有用的。这是示例的代码:
library(ggplot2)
map1 <- map_data('world')
map2 <- map_data('world2')
ggplot() +
theme_bw() +
geom_polygon(data = map1,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_fixed(1.4, xlim = c(100,380))
ggplot() +
theme_bw() +
geom_polygon(data = map2,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_fixed(1.4, xlim = c(100,380))
那么,世界地图上是否有像示例中的那些多边形但范围更大的多边形?
与其查找这些多边形,不如使用 coord_quickmap() 或 coord_sf() 裁剪经度边界。请注意 coord_map() 将错误地对多边形的点进行分组,除非在 ggplot() 调用之前被剪裁。
library(tidyverse)
ggplot() +
theme_bw() +
geom_polygon(data = map2,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_sf(xlim = c(20,100))
#coord_quickmap(xlim = c(100,20)) is also an alternative to coord_sf()
这很草率,但为什么不呢?您可以显示一个重复的世界,这样每个区域都可以显示出来而不会越过边缘。
library(dplyr)
map1 <- map_data('world')
map2 <- map_data('world') %>% mutate(long = long+360)
map3 <- map_data('world') %>% mutate(long = long+720)
ggplot() +
theme_bw() +
geom_polygon(data = map1,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
geom_polygon(data = map2,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
geom_polygon(data = map3,
mapping = aes(x = long,
y = lat,
group = group),
col = 'gray') +
coord_fixed(1.4, xlim = c(100,480))
“world”和“world2”是在“maps”包中实现的世界地图的两个版本。但是该包实际上允许您定义具有任何边界的世界地图,例如c(20,380) 或 c(100,460).
最简单的方法就是使用“wrap”选项(在 maps::map 的手册页中有描述)。此选项传递给 maps::map()(因此请查看地图包以获取完整帮助页面)
map3 <- map_data('world', wrap=c(20,380))
这应该会为您提供一张适合您选择的任何子午线的地图。请注意,在这种情况下,“wrap”必须是一个向量,并且左右边界必须正好相隔 360。