R 几何多边形&x2B;地图投影=莫名其妙地切割成两个形状?

R 几何多边形&x2B;地图投影=莫名其妙地切割成两个形状?,r,ggplot2,maps,R,Ggplot2,Maps,我正在尝试使用ggplot2在Winkel Tripel投影中绘制世界地图;它最终会有一些数据在上面。就我所知,ggplot不能进行Winkel Tripel,所以我用手动投影来解决这个问题。除了海洋层,我什么都有了,但是海洋层没有显示出来。代码: suppressPackageStartupMessages({ library(ggplot2) library(sp) library(rworldmap) library(rgdal) }) ll.to.wt &

我正在尝试使用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())
suppress-packagestartpmessages({
图书馆(GG2)
图书馆(sp)
图书馆(世界地图)
图书馆(rgdal)
})

ll.to.wt自
ggalt
以来,您可以直接进行基于项目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
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

所以,只要稍微调整一下:

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
lseq=seq(-53,85,by=.25)

边界虽然使用“ggalt”是“正确的答案”,但这里有一种方法可以得到你想要的——首先裁剪出南极洲(尽管我不支持这一点,但这真的很糟糕),然后使用“scale_y_discrete”。我对gg诗句不太了解,不知道为什么,但我认为连作打破了南部的界限,并以某种方式再次画出了两半之间的路径。离散种植可能会选择“最近的”坐标,因此保持南部边界

我们确实需要更好的工具来跨越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())
suppress-packagestartpmessages({
图书馆(GG2)
图书馆(sp)
图书馆(世界地图)
图书馆(rgdal)
})

我认为这可能是一个蝴蝶结多边形,即ll,ul,lr,ur,ll,而不是围绕ll,ul,ur,lr,ll的正确路径,这些表示上/下,左/右。不能测试我自己对吗now@mdsumner这就是lseq第二次被反转的原因。好吧,对不起,顺便说一句,这是缩放y_连续步骤,可能会以一种有问题的方式打断多边形-你可以通过注释这条线看到它很好(但对于y范围)out@mdsumner我在发帖前就试过了,但一点也没变。奇怪的在这篇文章完成后,我可能会玩得更多:)我怀疑子集+不使用scale\u y\u limit是真正的修复方法,更新了更多的信息/示例(超级糟糕的感冒正在发生,所以我昨晚太简短了…抱歉)。
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
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())