如何在GGPLATE中叠加空间地图上的经纬点? [英] How to overlay lat/lon points on a spatial map in ggplot?
本文介绍了如何在GGPLATE中叠加空间地图上的经纬点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在下面创建了一张欧洲地图,可以正常工作。
如何添加具有经度和经度的数据集(.csv格式)。我在下面创建了一个虚拟数据集,需要覆盖每个
Place
的经度,并为metricA
和metricB
分别标记x
和o
。有什么方法可以做到这一点吗?地图显示得很小,边距很大。我试图根据堆栈溢出R ggplot, remove white margins in ggsave/ggplot中的此示例添加
theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
,但在coord_sf(xlim = c(-40, 60), ylim = c(30, 90)) +
之前添加它时出现错误
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found
当我在后面添加它时,它不会改变页边距以使情节更大。我可以做些什么来使数字更大、边距更小?
library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
ggplot(data = world) +
geom_sf(color = "black", fill = "grey") +
coord_sf(xlim = c(-40, 60), ylim = c(30, 90)) +
ggtitle("Europe") +
xlab('Longitude') + ylab('Latitude'))
数据集:
Place MetricA MetricB Lat Lon
A x 55 -40
B x 60 -20
C x 75 18
D x x 68 3
E x 35 18
F x 74 42
G x 62 -26
H x 30 -30
I x x 69 15
推荐答案
您可以使用如下所示的点覆盖地图:
- 通过例如
tidyr::pivot_longer
将您的数据集转换为长格式,这样您的度量变量就成为一个变量的类别 - 通过
geom_point
将新指标变量映射到`Shape‘ 将点添加到地图
- 通过
scale_shape_manual
设置所需形状
关于你的第二个问题。我没能重现你的错误。调整地块页边距效果很好。但是,我不确定这是否足以在保存或打印时删除页边距。正如@OmriNewett已经指出的,问题在于地图是特殊的,因为长宽比是固定的,要去除页边距,您必须设置宽高比,以便与固定的长宽比匹配。
library(tidyr)
library(dplyr)
library("ggplot2")
library("sf")
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library("rnaturalearth")
library("rnaturalearthdata")
theme_set(theme_bw())
world <- ne_countries(scale = "medium", returnclass = "sf")
d <- read.table(text = "Place MetricA MetricB Lat Lon
A NA x 55 -40
B x NA 60 -20
C NA x 75 18
D x x 68 3
E NA x 35 18
F NA x 74 42
G x NA 62 -26
H NA x 30 -30
I x x 69 15", header = TRUE)
d <- d %>%
tidyr::pivot_longer(-c(Place, Lat, Lon), names_to = "Metric") %>%
filter(!is.na(value))
ggplot(data = world) +
geom_sf(color = "black", fill = "grey") +
geom_point(data = d, aes(x = Lon, y = Lat, shape = Metric)) +
scale_shape_manual(values = c(MetricA = 4, MetricB = 1)) +
theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) +
coord_sf(xlim = c(-40, 60), ylim = c(30, 90)) +
ggtitle("Europe") +
xlab('Longitude') + ylab('Latitude')
这篇关于如何在GGPLATE中叠加空间地图上的经纬点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文