WGS84坐标系统是全球定位系统(GPS)使用的一种地理坐标系统。WGS84是"World Geodetic System 1984"的缩写,是由美国国防部和美国国家地球空间情报局共同研制的。WGS84坐标系统使用经度和纬度来描述地球表面上的点和区域。它使用的是大地基准面,与传统的椭球体模型有所不同。WGS84坐标系统的零点定义为通过英国格林尼治天文台的经线(0度经线),也称为本初子午线。R的sf识别"EPSG:4326" or “WGS84” 都是该坐标系。
library(sf)library(ggplot2)library(ggspatial)# plotggplot(china)+geom_sf()+labs(title="Ministry of Civil of PRC",x="Lon",y="Lat")
1
2
3
4
5
6
#改变crs# plotggplot(china)+geom_sf()+coord_sf(crs="+proj=laea +lat_0=40 +lon_0=104")+labs(title="Ministry of Civil of PRC",x="Lon",y="Lat")
使用ggspatial包添加比例尺和指北针:
1
2
3
4
5
6
7
8
9
main_map=ggplot()+geom_sf(data=china,fill=NA,size=1,color="black")+coord_sf(crs="+proj=laea +lat_0=40 +lon_0=104")+labs(title="Ministry of Civil of PRC",x="Lon",y="Lat")+annotation_scale(location="bl")+# spatial-aware automagic north arrowannotation_north_arrow(location="tl",which_north="false",style=north_arrow_fancy_orienteering)+theme_minimal()main_map
#生成测试数据anno=data.frame(lon=runif(20,90,120),lat=runif(20,30,40),type=sample(letters[1:3],20,replace=TRUE),value=runif(20,1,10))#转换对象anno_sf<-st_as_sf(anno,coords=c("lon","lat"),crs=4326)p2=ggplot()+geom_sf(data=china,fill=NA,size=1,color="black")+#添加散点注释geom_sf(data=anno_sf,aes(fill=type,size=value),shape=21,colour='black',stroke=.25)+scale_size(range=c(1,5))+#添加textgeom_sf_text(data=anno_sf,aes(label=round(value,1)),size=2,legend="none")+coord_sf(crs="+proj=laea +lat_0=40 +lon_0=104")+labs(title="Ministry of Civil of PRC",x="Lon",y="Lat")+annotation_scale(location="bl")+# spatial-aware automagic north arrowannotation_north_arrow(location="tl",which_north="false",style=north_arrow_fancy_orienteering)+theme_minimal()p2
# Prepare the text for the tooltip (HTML style):gre_text<-paste("Type: ",anno$type,"<br/>","Value: ",round(anno$value,2),"<br/>")%>%lapply(htmltools::HTML)type_col=colorFactor(palette=c("red3","green3","blue3"),domain=anno$type)inter_p=leaflet(anno)%>%#添加图层addTiles()%>%#确定中心点setView(lng=104,lat=40,zoom=4)%>%#添加散点注释addCircleMarkers(~lon,~lat,fillColor=~type_col(type),fillOpacity=0.7,color="white",radius=8,stroke=FALSE,label=gre_text,labelOptions=labelOptions(style=list("font-weight"="normal",padding="3px 8px"),textsize="13px",direction="auto"))%>%#添加图例addLegend(pal=type_col,values=~type,opacity=0.7,title="Type",position="bottomright")class(inter_p)inter_p
# 将RasterLayer转换为sf对象sf_data<-st_as_sf(rasterToPolygons(china_tmp),crs=st_crs(china_tmp))colnames(sf_data)[1]="Average_temperature"ggplot()+geom_sf(data=china,fill=NA,size=1,color="black")+#添加栅格数据geom_sf(data=sf_data,aes(fill=Average_temperature),shape=22,size=0.5,stroke=0,color=NA)+scale_fill_gradient2(low="blue",mid="white",high="red")+#使用ggnewscale产生一个新的fill映射,否则会覆盖报错。ggnewscale::new_scale_fill()+#添加散点注释geom_sf(data=anno_sf,aes(fill=type,size=value),shape=21,colour='black',stroke=.25)+scale_size(range=c(1,5))+#添加textgeom_sf_text(data=anno_sf,aes(label=round(value,1)),size=2,legend="none")+coord_sf(crs="+proj=laea +lat_0=40 +lon_0=104")+labs(title="Ministry of Civil of PRC",x="Lon",y="Lat")+annotation_scale(location="bl")+# spatial-aware automagic north arrowannotation_north_arrow(location="tl",which_north="false",style=north_arrow_fancy_orienteering)+theme_minimal()