geom_polygon + 地图投影 = 莫名其妙的切成了两个形状?

geom_polygon + map projection = inexplicably cut into two shapes?

我正在尝试使用 ggplot2 在 Winkel Tripel 投影中绘制世界地图;它最终将在其之上有一些数据。据我所知,ggplot 本身不能做 Winkel Tripel,所以我已经用手动投影解决了这个问题。除了海洋层,我什么都有,结果不对。代码:

suppressPackageStartupMessages({
    library(ggplot2)
    library(sp)
    library(rworldmap)
    library(rgdal)
})
ll.to.wt <- function (points)
    as.data.frame(spTransform(SpatialPoints(points, CRS("+proj=longlat")),
                              CRS("+proj=wintri")))

world <- fortify(spTransform(getMap(), CRS("+proj=wintri")))
xlimits <- ll.to.wt(matrix(c(-180,180,0,0), nrow=2))$coords.x1
ylimits <- ll.to.wt(matrix(c(0,0,-60,85), nrow=2))$coords.x2
lseq = seq(-60, 85, by=.25)
boundary <- ll.to.wt(data.frame(
    long = c(rep(-180, length(lseq)), rep(180, length(lseq)), -180),
    lat  = c(lseq,                    rev(lseq),          lseq[1])))

ggplot() +
    geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3") +
    geom_map(data=world, map=world, aes(x=long, y=lat, map_id=id),
             color="#888888", fill="#f2caae", size=0.25) +
    scale_x_continuous(limits=xlimits, expand=c(0,0)) +
    scale_y_continuous(limits=ylimits, expand=c(0,0)) +
    coord_equal() +
    theme(
        axis.line=element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.justification = c(0,0), # bottom of box
        legend.position      = c(0,0), # bottom of picture
        panel.background=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        panel.margin=unit(0, "lines"),
        plot.background=element_blank())

渲染:

您可以看到原本应该是一个多边形填充的内容已被分割成两个单独的多边形,它们只覆盖地图的 "end caps",而不是中间。我如何让它填满整个地图?我认为问题出在 "boundary" 的定义上,但我在 geom_polygon 文档中没有看到任何内容来解释可能存在的问题。

自从 ggalt 您可以直接进行那些基于 PROJ.4 的投影:

library(ggplot2)
library(sp)
library(rworldmap)
library(rgdal)
library(ggalt)
library(ggthemes)

world <- fortify(getMap())
world <- subset(world, id != "Antarctica")
lseq = seq(-60, 85, by=.25)
boundary <- data.frame(
    long = c(rep(-180, length(lseq)), rep(180, length(lseq)), -180),
    lat  = c(lseq,                    rev(lseq),          lseq[1]))

gg <- ggplot()
gg <- gg + geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3")
gg <- gg + geom_map(data=world, map=world, 
                    aes(long, lat, map_id=id),
                    color="#888888", fill="#f2caae", size=0.25)
gg <- gg + coord_proj("+proj=wintri")
gg <- gg + theme_map()
gg

你原来的真正问题是你的多边形限制,因为你用你的边界框划破了南极洲的顶部:

lseq = seq(-53, 85, by=.25)
boundary <- data.frame(
    long = c(rep(-180, length(lseq)), rep(180, length(lseq)), -180),
    lat  = c(lseq,                    rev(lseq),          lseq[1]))

gg <- ggplot()
gg <- gg + geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3")
gg <- gg + geom_map(data=world, map=world, 
                    aes(long, lat, map_id=id),
                    color="#888888", fill="#f2caae", size=0.25)
gg <- gg + coord_proj("+proj=wintri")
gg <- gg + theme_map()
gg

虽然 coord_proj() 本来就没问题:

gg + coord_proj("+proj=wintri", ylim=c(-60, 85))

所以,稍微调整一下:

lseq = seq(-53, 85, by=.25)
boundary <- data.frame(
    long = c(rep(-180, length(lseq)), rep(180, length(lseq)), -180),
    lat  = c(lseq,                    rev(lseq),          lseq[1]))

gg <- ggplot()
gg <- gg + geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3")
gg <- gg + geom_map(data=world, map=world, 
                    aes(long, lat, map_id=id),
                    color="#888888", fill="#f2caae", size=0.25)
gg <- gg + coord_proj("+proj=wintri")
gg <- gg + theme_map()
gg

无需直接与企鹅交往也能正常工作:

gg <- ggplot()
gg <- gg + geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3")
gg <- gg + geom_map(data=world, map=world, 
                    aes(long, lat, map_id=id),
                    color="#888888", fill="#f2caae", size=0.25)
gg <- gg + coord_proj("+proj=wintri", ylim=c(-53, 85))
gg <- gg + theme_map()
gg

虽然使用 'ggalt' 是 "right answer",但这里有一种方法可以得到你想要的东西 - 首先裁剪出南极洲(虽然我不支持这个,但它真的很糟糕),然后使用'scale_y_discrete'。我对 gg-verse 不太熟悉,不知道为什么,但我收集到连续裁剪打破了南部边界,并以某种方式再次绘制了两半周围的路径。离散裁剪可能会选择 "nearest" 坐标,因此会保留南部边界。

我们确实需要更好的工具来跨越 sp/ggplot2 鸿沟,虽然我们有大量针对本地问题的部分修复程序,但两者没有适当的统一。 'ggalt' 是最好的之一。

suppressPackageStartupMessages({
library(ggplot2)
library(sp)
library(rworldmap)
library(rgdal)
})
ll.to.wt <- function (points)
as.data.frame(spTransform(SpatialPoints(points, CRS("+proj=longlat")),
                        CRS("+proj=wintri")))

## not much of a "world" without Antarctica . . 
badworld <- subset(spTransform(getMap(), CRS("+proj=wintri")), 
               !NAME == "Antarctica")
world <- fortify(badworld)
xlimits <- ll.to.wt(matrix(c(-180,180,0,0), nrow=2))$coords.x1
ylimits <- ll.to.wt(matrix(c(0,0,-60,85), nrow=2))$coords.x2
lseq = seq(-60, 85, by=.25)
boundary <- ll.to.wt(data.frame(
long = c(rep(-180, length(lseq)), rep(180, length(lseq)), -180),
 lat  = c(lseq,                    rev(lseq),          lseq[1])))

ggplot() +
geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3") + 
 geom_map(data=world, map=world, aes(x=long, y=lat, map_id=id),
       color="#888888", fill="#f2caae", size=0.25) +
scale_x_continuous(limits=xlimits, expand=c(0,0)) +
scale_y_discrete(limits=ylimits, expand=c(0,0)) +
coord_equal() +
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.justification = c(0,0), # bottom of box
legend.position      = c(0,0), # bottom of picture
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank())