如何使用ggplot2在地图上添加经度和纬度线?
How to add lines of longitude and latitude on a map using ggplot2?
我现在正在使用 ggplot2
绘制加拿大地图。因为默认的投影方式是"aea"(Albers Equal Area),所以经纬度在地图上都是直线。我想知道如何在地图上以“110W,100W,90W”和“50N,60N,70N”的形式显示经纬度。它们应该是曲线。非常感谢。
arcgis shapfile 从https://www.arcgis.com/home/item.html?id=dcbcdf86939548af81efbd2d732336db 下载
library(ggplot2)
library(rgdal)
countries<-readOGR("Canada.shp", layer="Canada")
ggplot()+geom_polygon(data=countries,aes(x=long,y=lat,group=group),fill='white',color = "black")
最后的结果应该是这样的。
您可以使用空间数据的单独经纬网图层,然后根据您的加拿大图层对其进行投影。
您可以在 NaturalEarthData.
找到免费的经纬网图层供下载
countries<-readOGR("Canada.shp", layer="Canada")
grat <- readOGR("graticule.shp", layer="graticule")
grat_prj <- spTransform(grat, CRS(countries))
ggplot() +
geom_polygon(data=countries, aes(x=long,y=lat,group=group),fill='white',color = "black") +
geom_path(data=grat_prj, aes(long, lat, group=group, fill=NULL), linetype="solid", color="grey50")
您可以使用 ggplot coord_map
参数 documented here
这使用投影来改变坐标网格。曲线将包括等距投影,但您应该查看 here 以获取所有允许投影的列表。选择哪一种是一种方式。
使用azequidistant
(我认为这是Azimuthal equidistant projection),并手动添加标签:
axis_labels <- rbind(
data.frame(long = rep(-140,5),lat = seq(40,80,10), labels = seq(40,80,10)), # x axis labels
data.frame(long = seq(-140,-60,40),lat = rep(85,3), labels = seq(140,60,-40)) # y axis labels
)
ggplot() +
geom_polygon(data=countries,aes(x=long,y=lat,group=group),fill='white',color = "black") +
coord_map("azequidistant") +
scale_x_continuous(breaks = seq(-140,60, by = 20))+
scale_y_continuous(breaks = seq(40,80, by = 10)) +
geom_text(data = axis_labels, aes(x = long, y = lat, label = labels)) +
theme_bw() +
theme(panel.grid.major = element_line(colour = "grey"),
panel.border = element_blank(),
axis.text = element_blank())
我现在正在使用 ggplot2
绘制加拿大地图。因为默认的投影方式是"aea"(Albers Equal Area),所以经纬度在地图上都是直线。我想知道如何在地图上以“110W,100W,90W”和“50N,60N,70N”的形式显示经纬度。它们应该是曲线。非常感谢。
arcgis shapfile 从https://www.arcgis.com/home/item.html?id=dcbcdf86939548af81efbd2d732336db 下载
library(ggplot2)
library(rgdal)
countries<-readOGR("Canada.shp", layer="Canada")
ggplot()+geom_polygon(data=countries,aes(x=long,y=lat,group=group),fill='white',color = "black")
最后的结果应该是这样的。
您可以使用空间数据的单独经纬网图层,然后根据您的加拿大图层对其进行投影。
您可以在 NaturalEarthData.
找到免费的经纬网图层供下载countries<-readOGR("Canada.shp", layer="Canada")
grat <- readOGR("graticule.shp", layer="graticule")
grat_prj <- spTransform(grat, CRS(countries))
ggplot() +
geom_polygon(data=countries, aes(x=long,y=lat,group=group),fill='white',color = "black") +
geom_path(data=grat_prj, aes(long, lat, group=group, fill=NULL), linetype="solid", color="grey50")
您可以使用 ggplot coord_map
参数 documented here
这使用投影来改变坐标网格。曲线将包括等距投影,但您应该查看 here 以获取所有允许投影的列表。选择哪一种是一种方式。
使用azequidistant
(我认为这是Azimuthal equidistant projection),并手动添加标签:
axis_labels <- rbind(
data.frame(long = rep(-140,5),lat = seq(40,80,10), labels = seq(40,80,10)), # x axis labels
data.frame(long = seq(-140,-60,40),lat = rep(85,3), labels = seq(140,60,-40)) # y axis labels
)
ggplot() +
geom_polygon(data=countries,aes(x=long,y=lat,group=group),fill='white',color = "black") +
coord_map("azequidistant") +
scale_x_continuous(breaks = seq(-140,60, by = 20))+
scale_y_continuous(breaks = seq(40,80, by = 10)) +
geom_text(data = axis_labels, aes(x = long, y = lat, label = labels)) +
theme_bw() +
theme(panel.grid.major = element_line(colour = "grey"),
panel.border = element_blank(),
axis.text = element_blank())