title: R语言ggplot绘制南极投影
date: 2021-08-21 10:15:52
tags: R语言
# 使用R语言的ggplot2包来绘制南极投影
# 方法一:带有颜色的
library(ggplot2)
world_data <- map_data("world") # ggplot2内的数据集
world_map <-
ggplot(world_data) +
geom_polygon(aes(x = long, y = lat, group = group, fill = region),
colour = "cyan", size = 0.5, show.legend = FALSE)
world_map + coord_map() # 默认墨卡托地图投影
# 正射投影
world_map + coord_map("ortho") # 默认北极为中心点。
# 南极为中心点
world_map + coord_map("ortho", orientation = c(-90, 0, 0),ylim = c(-90, -65))
# orientation中的第三个数是来设置地图旋转的角度的
# ylim和xlim是来设置经纬度范围的
world_map + coord_map("ortho", orientation = c(-90, 0, 30),ylim = c(-90, -65),xlim = c(-180, 180)) # 顺时针旋转30度
world_map + coord_map("ortho", orientation = c(41, -74, 0)) # 中心随便取一点
# 设置文件的路径和保存图片
# setwd('C:/Users/LHW/Desktop/')
# ggsave("x4.jpg", p , dpi = 600)
# 方法二:只有轮廓,自带的地图数据,分辨率比较低
world <- map_data("world")
worldmap <- ggplot(world, aes(x = long, y = lat, group = group)) +
geom_path() +
scale_y_continuous(breaks = c(-60,-75,-90) ) +
scale_x_continuous(breaks = c(-180,-120,-60,0,60,120,180) ,labels=NULL )+
theme(axis.ticks.x = element_blank())
# 其中的labels=NULL设置了不显示标签
# axis.ticks.x = element_blank()设置了不显示经度的刻度线
worldmap + coord_map("ortho", orientation = c(-90, 0, 0),ylim = c(-90, -65))
# 方法三:使用下载的高精度的shp文件来画
#加载shp文件
setwd('C:/Users/LHW/Desktop/ne_10m_land/')
#这个包是来读shp文件的
library(rgdal)
china_map1<- readOGR(".", "ne_10m_land") #shp文件需要放在当前工作路径下面
china_map1 <- fortify(china_map1) #将shp文件的数据格式转换称为数据框的形式
ggplot(data=china_map1, aes(x=long, y=lat, group=group))+
geom_polygon(fill="grey95", colour="grey60")+
coord_map("ortho", orientation = c(-90, 0, 0),ylim = c(-90, -65))
#存在的问题,画出的南极图中间会有一个去南极点的连线
#解决的方法,删去纬度为-90的点,基本是解决了
new_data<-china_map1[china_map1$lat>-85,]
ggplot(data=new_data, aes(x=long, y=lat, group=group))+
geom_polygon(fill="grey95", colour="grey60")+
coord_map("ortho", orientation = c(-90, 0, 0),ylim = c(-90, -65))
# 放大南极半岛区域
ggplot(data=new_data, aes(x=long, y=lat, group=group))+
geom_polygon(fill="grey95", colour="grey60")+
coord_map("azequidistant", orientation = c(-65, -60,0),ylim = c( -60,-70),xlim = c(-80, -40))
可参考的网页:
(37条消息) R_ggplot2地理信息可视化_史上最全(一)_R语言中文社区-CSDN博客
刻度、标签、散点大小等等精细化的设置可参考: