gpt4 book ai didi

r - geom_polygon + map projection = 莫名其妙的切成两个形状?

转载 作者:行者123 更新时间:2023-12-02 08:19:10 28 4
gpt4 key购买 nike

我正在尝试使用 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())

渲染:

world map drawn correctly except that the blue ocean fill only covers the "end caps" of the map, not the region in the middle

您可以看到原本应该是一个多边形填充的内容已被分割成两个单独的多边形,它们只覆盖 map 的“端盖”,而不是中间。我如何让它填满整个 map ?我认为问题出在“边界”的定义上,但我在 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

enter image description here

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

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

enter image description here

尽管 coord_proj() 无论如何都可以:

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

enter image description here

所以,稍微调整一下:

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

enter image description here

并且无需直接与企鹅交流也能正常工作:

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

enter image description here

关于r - geom_polygon + map projection = 莫名其妙的切成两个形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965571/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com