参考文章
https://blog.youkuaiyun.com/weixin_41929524/article/details/82810965
https://www.cnblogs.com/skyme/p/5182149.html
人口数据来源:http://www.stats.gov.cn/
地理坐标数据:http://www.chjunda.com/htdocs/gnjw/ningxia.htm
一、首先调用绘制中国地图需要的两个包以及ggplot2包
library(maps)
library(mapdata)
library(ggplot2)
二、运行一行代码便可以很方便的就绘制出中国地图
map(“china”, col = “red4”,xlim=c(70,140),ylim=c(0,60), panel.first = grid())
文章中通常不只是单纯的需要一幅地图,还需要添加其他数据,比如样地图等。那么如何绘制一幅添加其他数据的地图呢?接下来我们使用ggplot()进行图形绘制。
- 使用geom_path()函数生成中国地图轮廓
- 使用geom_point()函数将散点点在地图上,使用大小表示人口的大小
- 使用geom_text()函数添加城市的名称
- 使用theme()函数进行图形修饰
三、代码
library(openxlsx)
df <- read.xlsx(“Population.xlsx”,sheet = 1)
attach(df)
china <- map(“china”,plot=F)
ggplot()+
geom_path(data=china,aes(long,lat,group=group),color = ‘#FD9FA4’,show.legend = F) +
geom_point(data = df, aes(x = longitude, y = latitude,size=population), alpha = 0.8, color = ‘blue’) +
geom_text(data = df, aes(x = longitude, y = latitude, label = name), family = “STHeiti”) +
labs(x = ‘经度’, y = ‘纬度’, title = ‘中国31个省人口地图’, size = ‘人口’) +
theme_bw() +
theme(panel.border = element_blank(),
text = element_text(family = “STHeiti”),
plot.title = element_text(hjust = 0.5))
先不写了,大老板要找我谈话了,紧张紧张。
回来接着写啦——下面我们来修饰图形
原代码:
p<-p+ggplot()+
geom_path(data=china,aes(long,lat,group=group),color = ‘#FD9FA4’,show.legend = F) +
geom_point(data = df, aes(x = longitude, y = latitude,size=population), alpha = 0.8, color = ‘blue’) +
geom_text(data = df, aes(x = longitude, y = latitude, label = name), family = “STHeiti”) +
labs(x = ‘经度’, y = ‘纬度’, title = ‘中国31个省人口地图’, size = ‘人口’) +
theme_bw() +
theme(panel.border = element_blank(),
text = element_text(family = “STHeiti”),
plot.title = element_text(hjust = 0.5))
1.添加横纵坐标名称和图的名称
#方法一
p <- p+labs(x = ‘经度’, y = ‘纬度’, title = ‘中国31个省人口地图’, size = ‘人口’)
#方法二
p <- p+xlab(“经度”) + ylab(“纬度”) + ggtitle(“中国31个省人口地图”)
2.添加legend的名称
p <- p + guides(fill=guide_legend(title=“Legend_Title”))
3.设置范围
#方法一
p + scale_x_continuous(limits = c(70,140))+scale_y_continuous(limits = c(0,60))
#方法二
p + xlim(70,140)+ylim(0,60)
4.修改横轴和纵轴
p+theme(axis.title= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
5.仅修改横轴x
p+theme(axis.title.x= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
6.仅修改纵轴y
p+theme(axis.title.y= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
7.修改图片title
p+theme(title= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
8.修改图例legend
p+theme(legend.text= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
输出最后代码以及图片
ggplot()+
geom_path(data=china,aes(long,lat,group=group),color = ‘#FD9FA4’,show.legend = F) +
geom_point(data = df, aes(x = longitude, y = latitude,size=population), alpha = 0.8, color = ‘blue’) +
geom_text(data = df, aes(x = longitude, y = latitude, label = name), family = “STHeiti”) +
labs(x = ‘Longitude’, y = ‘Latitude’, title = ‘Population map of 31 provinces in china’, size = ‘population’) +
theme_bw() +
theme(panel.border = element_blank(),
text = element_text(family = “STHeiti”),
plot.title = element_text(hjust = 0.5))+theme(legend.position=c(0.9,0.5))+
theme(axis.text.x= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(axis.text.y= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(axis.title= element_text(size=18, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(legend.text= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(legend.title= element_text(size=18, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(title= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
**参考文章:**http://blog.sina.com.cn/s/blog_4a0824490102w4jm.html