require(maps)
require(ggmap)
library(mapdata)
library(maps)
library(ggplot2)
library(mapdata) 
library(autoimage) 
library(RColorBrewer)
library(ggplot2)
library(ggthemes)
# devtools::install_github("sjmgarnier/viridis") #这个好像没用到
library(viridis) 
#devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE)
library(ggmap)
library(scales)
library(grid)
library(dplyr)
library(gridExtra)
library("devtools")

1、根据经纬度坐标绘制地图

Get an API Key:

https://developers.google.com/maps/documentation/embed/get-api-key

TIPS:
需要绑定信用卡才有效

register_google(key = "xxx") #My key

数据准备

city_mean<-read.csv("city_mean.csv",fileEncoding ="GBK")
colnames(city_mean)<-c("city","num","longitude","latitude","pp")

数据1

colormap <- c("Violet","Blue","Green","Yellow","Red")
MyMap <- get_map(location = "China", 
                 source = "google", maptype = "roadmap", crop = FALSE, zoom = 4)
ggmap(MyMap) %+% city_mean +
  aes(x = longitude, y = latitude, z = pp) +
  stat_summary_2d(fun = median, binwidth = c(.5, .5), alpha = 0.5) +
  scale_fill_gradientn(name = "Median", colours = colormap, space = "Lab") +
  labs(x = "Longitude", y = "Latitude") +
  coord_map()

地图1

2、根据省份数据绘制地图

#######地图#########
library(mapdata)
library(maptools)
library(ggplot2)
library(plyr)
library(rgdal)
library(sf)
library(maps)
library(mapproj)
china_map=readShapePoly("bou2_4p.shp") #可以在网上下载该文件:https://github.com/GuangchuangYu/chinamap/blob/master/inst/extdata/china/bou2_4p.shp
plot(china_map)
ggplot(china_map,aes(x=long,y=lat,group=group))+
  geom_polygon(fill="white",colour="black")+
  coord_map("polyconic")+
  theme(
    panel.grid=element_blank(),
    panel.background=element_blank(),
    axis.text=element_blank(),
    axis.ticks=element_blank(),
    axis.title=element_blank(),
    legend.position=c(0.2,0.3)
  )
x<-china_map@data
xs<-data.frame(x,id=seq(0:924)-1)#地图中共计有925个地域信息
china_map1<-fortify(china_map)
china_map_data<-join(china_map1,xs,type="full")#基于id进行连接
aa<-unique(china_map@data$NAME)#查看地图数据中保存的地域名称,编辑自己的数据与其一致
an<-iconv(aa,from='gbk',to='UTF8')
mydata<-read.csv("province.csv",header=T,as.is=T)
colnames(mydata)<-c("NAME","counts")

数据2

aa<-china_map_data$NAME
an<-iconv(aa,from='gbk',to='UTF8') # 转化下格式,不用换的请忽略
china_map_data$NAME<-an
china_data <- join(china_map_data, mydata, type="full")#基于NAME字段进行连接,NAME字段来自于地图文件中
ggplot(china_data,aes(x=long,y=lat,group=group,fill=counts))+
  geom_polygon(colour="grey40")+
  scale_fill_gradient(low="white",high="red")+
  coord_map("polyconic")+
  theme(
    panel.grid=element_blank(),
    panel.background=element_blank(),
    axis.text=element_blank(),
    axis.ticks=element_blank(),
    axis.title=element_blank(),
    legend.position=c(0.2,0.3)
  )##参数“ratio”为我们要展现的数据指标,基于该指标绘制热力图
  #加省名标签midpos
midpos<- function(x) mean(range(x,na.rm=TRUE)) 
centres<- ddply(china_data,.(NAME),colwise(midpos,.(long,lat)))
centres[,1]<-c("Shanghai","Yunnan","Neimenggu","Beijing","Taiwan","Jilin","Sichuan","Tianjin","Ningxia","Anhui","Shandong","Shanxi","Guangdong","Guangxi","Xinjiang","Jiangsu","Jiangxi","Hebei","Henan","Zhejiang","Hainan","Hubei","Hunan","Gansu","Fujian","Xizang","Guizhou","Liaoning","Chongqing","Shanxi","Qinghai","Xianggang","Heilongjiang","Aomen")
#以下是个别省份的名称显示的有些偏斜,手动调整让省份名字居中
#centres[24,2]<- (centres[24,2]+2) 
#centres[18,2]<- (centres[18,2]-1)
#centres[18,3]<- (centres[18,3]-1)
#centres[13,3]<- (centres[13,3]+1)
#centres[13,2]<- (centres[13,2]+1)
#centres[16,2]<- (centres[16,2]+1)
#centres[3,3]<- (centres[3,3]-2)
#centres[21,3]<- (centres[21,3]-5)
#centres[21,2]<- (centres[21,2]-2)
colnames(china_data)[ncol(china_data)]<-"Number"
ggplot(china_data,aes(long,lat))+           
  geom_polygon(aes(group=group,fill=Number),colour="black")+    
  scale_fill_gradient(low="white",high="blue",na.value = "blue") +     
  coord_map("polyconic") +     
  geom_text(aes(label=NAME),data=centres,size=1.8)+    
  theme( panel.grid = element_blank(),   panel.background = element_blank(),axis.text = element_blank(), axis.ticks = element_blank(),         
         axis.title = element_blank()  ,
         legend.position=c(0.9,0.4),
         legend.key.height=unit(1.5, 'line')
         )
  

在这里插入图片描述

3、城市级别地图:

python:

pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
import pyecharts
pip install echarts-china-cities-pypkg
from pyecharts.charts import Geo
from pyecharts.charts import Map
import statsmodels.api as sm
import statsmodels.formula.api as smf
import pandas as pd
import numpy as np
city_mean=pd.read_csv("aa_tem.csv")
city_mean.head()
a1=city_mean['city'].tolist()
a2=city_mean['pp'].tolist()
from pyecharts import options as opts
from pyecharts.charts import Map
geo=Map()
geo.add("geo",[list(z) for z in zip(a1, a2)],"china-cities" ,is_map_symbol_show=False)
geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo.set_global_opts(visualmap_opts=opts.VisualMapOpts(is_piecewise=True,max_=1),title_opts=opts.TitleOpts(title="Geo-基本示例"))
geo.render_notebook()#

在这里插入图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐