1.加载包
library(plyr)
library(maps)
library(mapdata)
library(leaflet)
library(stringi)
library(maptools)
library(htmltools)
library(RColorBrewer)
library(ggplot2)
2.载入路径
getwd()
setwd("D:/本人信息/生信小书生/R语言绘图/2022-3-19-地图/知乎杜雨数据/DataWarehouse-master/Rstudy/散点地图")
3.导入地图素材数据导入以及编造数据
province_city <- read.csv("D:/本人信息/生信小书生/R语言绘图/2022-3-19-地图/知乎杜雨数据/DataWarehouse-master/Rstudy/Province/chinaprovincecity.csv") #中国各省省会坐标
province_city$size<-round(runif(34,5,10),2) #添加连续数值变量
province_city$type<-factor(sample(LETTERS[1:5],34,replace=TRUE)) #添加因子变量以便之后演示
co<-substr(rainbow(34),1,7)
province_city<-data.frame(province_city,co)
4.调节颜色
pal <- colorFactor(c("green", "red","yellow","green","blue"), domain = c("A", "B","C","D","E"))#因子变量的映射色盘
cPal <- colorNumeric(palette = c("blue","yellow","red"),domain =province_city$size) #数值型变量的映射色盘
5.绘图
#1
leaflet(province_city)%>%addTiles()%>% addMarkers(lng=~jd,lat=~wd,popup=~city)
#2
leaflet(province_city)%>%addTiles()%>% addCircles(lng=~jd,lat=~wd,color="red",weight=~size,popup=~city)
#3
leaflet(province_city)%>%addTiles()%>% addCircleMarkers(lng=~jd,lat=~wd,radius=~size, color = ~co, fill=TRUE,popup=~city)
#4
leaflet(province_city)%>%addTiles()%>% addCircles(lng=~jd,lat=~wd,color = ~pal(type),weight=~size,popup=~city)%>%
addLegend("bottomright",pal=pal,values = ~type,title = "GDP等级",labFormat = labelFormat(suffix = "档"),opacity=1)
leaflet(province_city)%>%addTiles()%>% addCircleMarkers(lng=~jd,lat=~wd,color = ~pal(type),radius = ~size,fill=TRUE,popup=~city)
#5
leaflet(province_city)%>%addTiles()%>%
addCircleMarkers(lng=~jd,lat=~wd,fillColor=~cPal(size),radius = ~size,stroke=FALSE,fillOpacity=0.8,popup=~city)%>%
addLegend("bottomright",pal=cPal,values = ~size,title = "GDP等级",labFormat = labelFormat(suffix = "档"),opacity=1)
#6
leaflet(province_city)%>%addProviderTiles("Esri.WorldStreetMap")%>%addMarkers(lng=~jd,lat=~wd,popup=~city)
#7
leaflet(province_city)%>%addProviderTiles("Esri.WorldImagery")%>%addMarkers(lng=~jd,lat=~wd,popup=~city)
#8
leaflet(province_city)%>%addProviderTiles("Esri.WorldTerrain")%>%addMarkers(lng=~jd,lat=~wd,popup=~city)
#9
leaflet(province_city)%>%addProviderTiles("Esri.NatGeoWorldMap")%>%addMarkers(lng=~jd,lat=~wd,popup=~city)
#10
leaflet(province_city)%>%addProviderTiles("Esri.WorldStreetMap")%>%addCircles(lng=~jd,lat=~wd,color="red",weight=~size,popup=~city)
#11
leaflet(province_city)%>%addProviderTiles("Esri.WorldImagery")%>%addCircles(lng=~jd,lat=~wd,color="red",weight=~size,popup=~city)
#12
leaflet(province_city)%>%addProviderTiles("Esri.WorldTerrain")%>%addCircles(lng=~jd,lat=~wd,color="red",weight=~size,popup=~city)
#13
leaflet(province_city)%>%addProviderTiles("Esri.NatGeoWorldMap")%>%addCircles(lng=~jd,lat=~wd,color="red",weight=~size,popup=~city)
#14
leaflet(province_city)%>%addProviderTiles("Esri.WorldStreetMap")%>%addCircleMarkers(lng=~jd,lat=~wd,radius=~size,color=~co,fill=TRUE,popup=~city)
#15
leaflet(province_city)%>%addProviderTiles("Esri.WorldImagery")%>%addCircleMarkers(lng=~jd,lat=~wd,radius=~size,color=~co,fill=TRUE,popup=~city)
#16
leaflet(province_city)%>%addProviderTiles("Esri.WorldTerrain")%>%addCircleMarkers(lng=~jd,lat=~wd,radius=~size,color=~co,fill=TRUE,popup=~city)
#17
leaflet(province_city)%>%addProviderTiles("Esri.NatGeoWorldMap")%>%addCircleMarkers(lng=~jd,lat=~wd,radius=~size,color=~co,fill=TRUE,popup=~city)