- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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())
渲染:
您可以看到原本应该是一个多边形填充的内容已被分割成两个单独的多边形,它们只覆盖 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
你原来的真正问题是你的多边形限制,因为你用你的边界框划破了南极洲的顶部:
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
关于r - geom_polygon + map projection = 莫名其妙的切成两个形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965571/
我引用了 this question 的答案并有其他问题。 我修改了如下代码: library(ggplot2) ids <- letters[1:2] # IDs and values to use
此代码生成一个包含 3 个多边形的图表... 我正在创建一个显示 3 个多边形的图表,如果有更好的方法来绘制多边形(实际上这些多边形代表事件并且这些事件有持续时间),我不是很感兴趣。 首先,我感兴趣的
更新: 我已经解决了我的问题。我在找 coord_cartesian(xlim = c(800, 2100), ylim = c(0, 0.0021)) 感谢所有试图提供帮助的人! 问题是: 我想很好
我需要创建一张欧洲 map 来显示某个变量在各个国家/地区的分布情况。我需要黑白 map 。我依靠 ggplot 并遵循了这个 approach举个例子。我根据 this blogpost 更改了图例
我正在尝试使用 geom_point 生成散点图其中点由平滑多边形外接,geom_polygon . 这是我的点数据: set.seed(1) df = x) return(data.fram
请在下面找到 My data nd。 nd 包含两列 > nd lnd y 1: 0.0 0.350 2: 0.1 0.351 3: 0.2 0.
您好,我需要一些帮助来绘制一个简单的三角形 library(ggplot2) library(data.table) dt.triangle <- data.table(group = c(1,1,1
我对 R 很陌生,最近一直在尝试使用以下代码在 ggplot2 中创建英国的轮廓: library(ggplot2) UK <- map_data("world2Hires", region = "U
我正在使用 R/ggplot 创建包含世界地震带的全局 map 。我正在使用来自此处的修改后的 shapefile:http://gmo.gfz-potsdam.de/ . 地 block 共有9层,
我正在绘制下表的美国多重耐药性趋势: MDR by region 使用以下代码: states_map<-map_data('state') m <- ggplot(ncftrendsort, aes
一位学生问我是否可以使用 R 重新创建类似于下面的图: 这是来自this paper.... 这类东西不是我的专长,但使用以下代码我能够创建 95% CI 椭圆并使用 geom_polygon() 绘
我正在尝试使用 ggplot2 在 Winkel Tripel 投影中绘制世界地图;它最终将在其之上有一些数据。据我所知,ggplot 本身不能做 Winkel Tripel,所以我已经用手动投影解决
我的数据集: Taxa dn dc Cha 10.2 -20.4 Cha 10.7 -19.7 Cha 4.9 -21.0 Cha 5.4 -20.6 Cha
有questions关于 ggplot2 的事实无法绘制具有 的多边形形状孔 . 这是因为,如果点的顺序不正确,则最终图看起来很糟糕,通常在 donut 形状内带有剪裁/修剪线。 我已经阅读了很多关于
我正在使用 geom_polygon 叠加数据,但无法区分颜色,因为它们合并在一起。如果层数更多,情况会更糟。 如何确保颜色不会混合? 示例数据 newdat <- structure(list(x
我正在尝试使用分面来生成多个填充不同值的 map 。 我创建了下面的简化示例,它重现了我想要做的事情以及我不期望从 ggplot 得到的结果。我使用美国 map 并为各州生成两个假设的社区。我可以单独
我有一个带有推文坐标的数据框,例如: library(ggplot2) df <- data.frame(long = c(-58.1, -58.2, -58.3, -58.4, -58.5, -55
说我有这些数据。一个用于对州的县进行着色,另一个用于绘制点。 library(tidyverse) library(gganimate) devtools::install_github("Urban
我正在使用 map 库和 ggplot 的 geom_polygon 创建 map 。我只是想将默认的蓝色、红色、紫色配色方案更改为其他颜色。我对 ggplot 非常陌生,所以如果我没有使用正确的数据
我正在遵循示例here为我的 ggplot 对象创建自定义工具提示。我不想使用plotly,因为它的渲染方式很 Shiny 。 我让它生成包含我想要的内容的工具提示,但它没有返回正确的近点。我注意到
我是一名优秀的程序员,十分优秀!