|
本例子中的代码已经不推荐使用,tmap版本的地图示例参见 http://blog.sciencenet.cn/blog-255662-1144743.html
如果所示: 用R绘制的采样点地图——带指北针、比例尺、图例等
更多请参考 https://news-at.zhihu.com/story/9275383
源代码和数据下载 用R绘制采样点地图.zip
setwd("/Users/jinlong/world map")
library(maps) ## 提供绘制世界地图的函数
library(mapdata) ## 提供世界地图
library(openxlsx) ## 读取xlsx文件
library(ggplot2) ## 绘图核心函数
library(ggsn) ## 提供指北针
library(legendMap) ## 提供比例尺
### devtools::install_github("3wen/legendMap")
library(maptools) ## 提供readShapePoly函数, 读取中国多边形数据
### Download https://simplemaps.com/static/data/world-cities/basic/simplemaps-worldcities-basic.csv
china0 <-readShapePoly("bou1_4p") ## 读取中国地图
china_df <-fortify(china0) ## 将list转换为ggplot可以使用的dataframe
##读取数据https://simplemaps.com/static/data/world-cities/basic/simplemaps-worldcities-basic.csv
cities <-read.csv("simplemaps-worldcities-basic.csv")
cities <-cities[sample(1:nrow(cities),size =200), ] ## 元数据有7000条, 这里随机选出100条
pdf(file ="sampling sites location.pdf",width =11.5,height =5.8)
### tiff(filename = "sampling sites location.tiff",
### width = 7200, height = 3600, res = 600,
### compression = "lzw") ## 如果输出tiff文件,600dpi,宽度7200像素,高度3600像素
rrr <-ggplot()+
borders(colour='darkgrey')+ ##添加世界地图
geom_polygon(data =china_df,
aes(x =long, y =lat, group =group), colour ="darkgrey",
fill ="white")+ ##添加中国地图
geom_point(data =cities,
aes(x=lng,y=lat,colour=pop),size =2)+ ##点的颜色代表人口
theme_bw()+ ##简约风格主题
ggtitle("Population of World Cities")+ ##标题
scale_bar(lon =100,lat =-60,
distance_lon =1000,distance_lat =200,
distance_legend =500,dist_unit ="km",
arrow_length =0,arrow_distance =0,
arrow_north_size =0)+ ##比例尺
xlab("Longitude (Degree)")+ ##横坐标
ylab("Latitude (Degree)")+ ##纵坐标
scale_colour_gradientn(colours=c("green","yellow","orange","red"))
north2(rrr,.82,.88) ## 数字是指定指北针的位置
dev.off()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-19 11:30
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社