I'm trying to plot a map of the Pacific using World2Hires in R's mapproj library but there's an odd glitch when I try to fill the countries. How do I fix this?
我正试图在R的mapproj图书馆中使用World2Hires绘制太平洋地图,但是当我试图填补这些国家时,这是一个奇怪的故障。我该如何解决?
library(maps)
library(mapproj)
library(mapdata)
map("world2Hires",
xlim=c(120, 260),
ylim=c(-60, 40),
boundary=TRUE,
interior=TRUE,
fill=TRUE,
col="gray30",
)
map.axes()
Here's the output:
这是输出:
2 个解决方案
#1
7
The issue seems to be with a small subset of areas which cause wrapping. From some trial and error saving the original map
call like mapnames <- map(...)
and then passing subsets of this list to the regions=
argument in a new call, I could avoid the wrapping around of the fills. E.g.:
问题似乎是导致包装的一小部分区域。从一些试验和错误中保存原始地图调用,如mapnames < - map(...),然后将此列表的子集传递给新调用中的regions =参数,我可以避免填充的环绕。例如。:
library(maps)
library(mapproj)
library(mapdata)
map("world2Hires", regions=mapnames$names[c(1:7,14:641)],
xlim=c(120, 260),
ylim=c(-60, 40),
boundary=TRUE,
interior=TRUE,
fill=TRUE
)
map.axes()
As to a more thorough or sensible solution to prevent this happening, I am stumped. Playing with the wrap=
option does nothing helpful, and likewise for the other options. As a side-note, this issue does not appear using the "world"
database, but only pops up for "world2"
and "world2Hires"
.
至于防止这种情况发生的更全面或明智的解决方案,我感到难过。使用wrap =选项没有任何帮助,同样适用于其他选项。作为旁注,这个问题并没有出现在使用“世界”数据库,而只是弹出“world2”和“world2Hires”。
#2
5
The answer by @thelatemail is the best and simplest solution I've seen to this problem. To make it more universal, though, it is better to remove the polygons by name. This is because depending on the limits you give your first call to map(), the indices of the polygon names can be different.
@thelatemail的答案是我见过的最好,最简单的解决方案。但是,为了使其更具通用性,最好按名称删除多边形。这是因为根据您第一次调用map()的限制,多边形名称的索引可能不同。
library(maps)
library(mapproj)
library(mapdata)
mapnames <- map("world2Hires", xlim=c(120, 260), ylim=c(-60, 40),
fill=TRUE, plot=FALSE)
mapnames2 <- map("world2Hires", xlim=c(100, 200), ylim=c(-20, 60),
fill=TRUE, plot=FALSE)
mapnames$names[10]
[1] "Mali"
mapnames2$names[10]
[1] "Thailand"
There are 8 countries that are intersected by the Prime Meridian: United Kingdom, France, Spain, Algeria, Mali, Burkina Faso, Ghana, and Togo. By matching these country names with mapnames$names
, you can remove the polygons regardless of your original extent:
Prime Meridian有8个国家:英国,法国,西班牙,阿尔及利亚,马里,布基纳法索,加纳和多哥。通过将这些国家/地区名称与地图名称$ names匹配,您可以删除多边形,而不管原始范围如何:
remove <- c("UK:Great Britain", "France", "Spain", "Algeria", "Mali",
"Burkina Faso", "Ghana", "Togo")
map("world2Hires", regions=mapnames$names[!(mapnames$names %in% remove)],
xlim=c(120, 260),
ylim=c(-60, 40),
boundary=TRUE,
interior=TRUE,
fill=TRUE
)
map.axes()
You could also use grepl() but because polygons are named heirarchically, you may remove some sub-polygons of the nations in question. For example, mapnames$names[grepl("UK", mapnames$names)]
returns 34 matches.
您也可以使用grepl(),但由于多边形是以层次结构命名的,因此您可以删除相关国家的一些子多边形。例如,mapnames $ names [grepl(“UK”,mapnames $ names)]返回34个匹配项。
I would have suggested this as an edit, but I don't have privileges yet.
我本来建议这是一个编辑,但我还没有特权。
#1
7
The issue seems to be with a small subset of areas which cause wrapping. From some trial and error saving the original map
call like mapnames <- map(...)
and then passing subsets of this list to the regions=
argument in a new call, I could avoid the wrapping around of the fills. E.g.:
问题似乎是导致包装的一小部分区域。从一些试验和错误中保存原始地图调用,如mapnames < - map(...),然后将此列表的子集传递给新调用中的regions =参数,我可以避免填充的环绕。例如。:
library(maps)
library(mapproj)
library(mapdata)
map("world2Hires", regions=mapnames$names[c(1:7,14:641)],
xlim=c(120, 260),
ylim=c(-60, 40),
boundary=TRUE,
interior=TRUE,
fill=TRUE
)
map.axes()
As to a more thorough or sensible solution to prevent this happening, I am stumped. Playing with the wrap=
option does nothing helpful, and likewise for the other options. As a side-note, this issue does not appear using the "world"
database, but only pops up for "world2"
and "world2Hires"
.
至于防止这种情况发生的更全面或明智的解决方案,我感到难过。使用wrap =选项没有任何帮助,同样适用于其他选项。作为旁注,这个问题并没有出现在使用“世界”数据库,而只是弹出“world2”和“world2Hires”。
#2
5
The answer by @thelatemail is the best and simplest solution I've seen to this problem. To make it more universal, though, it is better to remove the polygons by name. This is because depending on the limits you give your first call to map(), the indices of the polygon names can be different.
@thelatemail的答案是我见过的最好,最简单的解决方案。但是,为了使其更具通用性,最好按名称删除多边形。这是因为根据您第一次调用map()的限制,多边形名称的索引可能不同。
library(maps)
library(mapproj)
library(mapdata)
mapnames <- map("world2Hires", xlim=c(120, 260), ylim=c(-60, 40),
fill=TRUE, plot=FALSE)
mapnames2 <- map("world2Hires", xlim=c(100, 200), ylim=c(-20, 60),
fill=TRUE, plot=FALSE)
mapnames$names[10]
[1] "Mali"
mapnames2$names[10]
[1] "Thailand"
There are 8 countries that are intersected by the Prime Meridian: United Kingdom, France, Spain, Algeria, Mali, Burkina Faso, Ghana, and Togo. By matching these country names with mapnames$names
, you can remove the polygons regardless of your original extent:
Prime Meridian有8个国家:英国,法国,西班牙,阿尔及利亚,马里,布基纳法索,加纳和多哥。通过将这些国家/地区名称与地图名称$ names匹配,您可以删除多边形,而不管原始范围如何:
remove <- c("UK:Great Britain", "France", "Spain", "Algeria", "Mali",
"Burkina Faso", "Ghana", "Togo")
map("world2Hires", regions=mapnames$names[!(mapnames$names %in% remove)],
xlim=c(120, 260),
ylim=c(-60, 40),
boundary=TRUE,
interior=TRUE,
fill=TRUE
)
map.axes()
You could also use grepl() but because polygons are named heirarchically, you may remove some sub-polygons of the nations in question. For example, mapnames$names[grepl("UK", mapnames$names)]
returns 34 matches.
您也可以使用grepl(),但由于多边形是以层次结构命名的,因此您可以删除相关国家的一些子多边形。例如,mapnames $ names [grepl(“UK”,mapnames $ names)]返回34个匹配项。
I would have suggested this as an edit, but I don't have privileges yet.
我本来建议这是一个编辑,但我还没有特权。