R|ggplot2 画中国省会地图
程序员文章站
2022-06-08 12:57:40
...
小白在实验室打杂 被boss要求画一个省会地图
虽然一开始对R什么都不会,但是整体感觉挺像matlab的
下面写一下我自己作为一个小白遇到的天坑
首先 要先装一个R for windows(我的实验室台机是win10的)
下载R
点击 download 然后我的源是Elite Education
安装时就按默认路径C:\Program Files\R
会给你装一个32位和一个64位的RGUI
然而并没有什么卵用 因为不方便-
然后 我们下一个好用的可视化工具 Rstudio
不用不是中国人
就下desktop free那个版本的 默认路径安装 没问题的
然后打开Rstudio以上 我们整体环境就搭好了
- 下面 我开始碰到各种天坑
- 首先安装R包
- ggplot2
在命令行窗口使用语句 install.packages(“ggplot2”)
或者是在右下角点install 然后输入ggplot2
但是你会遇到各种各样的问题 一切都是因为源不好 强烈推荐合肥大学的源
在tools-global options-packages-选合肥那个源 然后再install.packages(“ggplot2”) 你会体会到 飞一样的速度(清北那两个源垃圾),一定要把各种dependencies下了 - maptools天坑
- ggplot2
- 首先安装R包
> install.packages("maptools")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
also installing the dependency ‘sp’
trying URL 'https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.4/sp_1.2-5.zip'
Content type 'application/zip' length 1538246 bytes (1.5 MB)
downloaded 1.5 MB
trying URL 'https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.4/maptools_0.9-2.zip'
Content type 'application/zip' length 1823828 bytes (1.7 MB)
downloaded 1.7 MB
package ‘sp’ successfully unpacked and MD5 sums checked
package ‘maptools’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Administrator\AppData\Local\Temp\Rtmpm4yN7i\downloaded_packages
> library("maptools")
载入需要的程辑包:sp
Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
> install.packages("sp")
Error in install.packages : Updating loaded packages
Restarting R session...
> install.packages("sp")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.4/sp_1.2-5.zip'
Content type 'application/zip' length 1538246 bytes (1.5 MB)
downloaded 1.5 MB
package ‘sp’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Administrator\AppData\Local\Temp\Rtmpi6lDH3\downloaded_packages
> library("maptools")
载入需要的程辑包:sp
Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
> gpclibPermit()
[1] FALSE
> gpclibPermitStatus()
[1] FALSE
> install.packages("gpclib")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Package which is only available in source form, and
may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
> install.packages("gpclib")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Package which is only available in source form, and
may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
> install.packages("gpclib")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Package which is only available in source form, and
may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
> install.packages("gpclib", dependencies = FALSE)
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Package which is only available in source form, and
may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
> install.packages("gpclib", dependencies = FALSE)
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Package which is only available in source form, and
may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
> install.packages('gpclib',repo='http://nbcgib.uesc.br/mirrors/cran/')
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
> install.packages("rgeos")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/rgeos_0.3-25.zip'
Content type 'application/zip' length 1729615 bytes (1.6 MB)
downloaded 1.6 MB
package ‘rgeos’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Administrator\AppData\Local\Temp\Rtmpi6lDH3\downloaded_packages
> library("maptools")
解释下: 我要用maptools 还有依赖包sp 都装好了 但是报错
Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
> gpclibPermit()
[1] FALSE
> gpclibPermitStatus()
[1] FALSE
> install.packages("gpclib")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Package which is only available in source form, and
may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
我去*找到解决办法 1.装gpclib 我换了n多个源 全部失败了2.装rgeos 成功了
然后按这位大佬的代码和方法 就很完美了
还有一个天坑就是:画的时候说你的region太大了 其实你把rstudio最大化就行了 真的很坑
后续从maptools开始的
> library("maptools")
> china_map = readShapePoly("C:\rstudy\china-province-border-data\bou2_4p.shp")
Error: '\c' is an unrecognized escape in character string starting ""C:\rstudy\c"
> china_map = readShapePoly("C:/rstudy/china-province-border-data/bou2_4p.shp")
> plot(china_map)
Error in plot.new() : plot region too large
> plot(china_map)
> library(ggplot2)
> ggplot(china_map,aes(x=long,y=lat,group=group)) +
+ geom_polygon(fill="white",colour="grey") +
+ coord_map("polyconic")
Regions defined for each Polygons
Error in loadNamespace(name) : 不存在叫‘mapproj’这个名字的程辑包
> install.packages("mapproj")
Installing package into ‘D:/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/mapproj_1.2-5.zip'
Content type 'application/zip' length 85463 bytes (83 KB)
downloaded 83 KB
package ‘mapproj’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Administrator\AppData\Local\Temp\Rtmpi6lDH3\downloaded_packages
> ggplot(china_map,aes(x=long,y=lat,group=group)) +
+ + geom_polygon(fill="white",colour="grey") +
+ + coord_map("polyconic")
Regions defined for each Polygons
Error in +geom_polygon(fill = "white", colour = "grey") :
invalid argument to unary operator
> ggplot(china_map,aes(x=long,y=lat,group=group)) +geom_polygon(fill="white",colour="grey") +coord_map("polyconic")
Regions defined for each Polygons
> x <- aaa@qq.com
> xs <- data.frame(x,id=seq(0:924)-1) #含岛屿共925个形状
> library(ggplot2)
> china_map1 <- fortify(china_map) #转化为数据框
Regions defined for each Polygons
> library(plyr)
> china_map_data <- join(china_map1, xs, type = "full")
Joining by: id
> unique(aaa@qq.com$NAME)
[1] 黑龙江省 内蒙古自治区 **自治区
[4] 吉林省 辽宁省 甘肃省
[7] 河北省 北京市 山西省
[10] 天津市 陕西省 宁夏回族自治区
[13] 青海省 山东省 *自治区
[16] 河南省 江苏省 安徽省
[19] 四川省 湖北省 重庆市
[22] 上海市 浙江省 湖南省
[25] 江西省 云南省 贵州省
[28] 福建省 广西壮族自治区 *省
[31] 广东省 香港特别行政区 海南省
[34] <NA>
33 Levels: 安徽省 北京市 福建省 甘肃省 ... 重庆市
> mydata <- read.csv("c:/rstudy/geshengzhibiao.csv")
> china_data <- join(china_map_data, mydata, type="full")
Joining by: NAME
> ggplot(china_data, aes(x = long, y = lat, group = group, fill = zhibiao)) +geom_polygon(colour="grey40") +scale_fill_gradient(low="white",high="steelblue") + coord_map("polyconic")
> ggplot(china_data, aes(x = long, y = lat, group = group,fill = zhibiao)) +
+ geom_polygon(colour="grey40") +
+ scale_fill_gradient(low="white",high="steelblue") + #指定渐变填充色,可使用RGB
+ coord_map("polyconic") + #指定投影方式为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)
+ )
> midpos <- function(x) mean(range(x,na.rm=TRUE)) #取形状内的平均坐标
> centres <- ddply(china_data,.(province),colwise(midpos,.(long,lat)))
>
> ggplot(china_data,aes(long,lat))+ #此处语法与前面不同,参考ggplot2一书P85
+ geom_polygon(aes(group=group,fill=zhibiao),colour="black")+
+ scale_fill_gradient(low="white",high="steelblue") +
+ coord_map("polyconic") +
+ geom_text(aes(label=province),data=centres) +
+ theme(
+ panel.grid = element_blank(),
+ panel.background = element_blank(),
+ axis.text = element_blank(),
+ axis.ticks = element_blank(),
+ axis.title = element_blank()
+ )
> province_city <- read.csv("c:/rstudy/chinaprovincecity.csv") #读取省会城市坐标
>
> ggplot(china_data,aes(long,lat))+
+ geom_polygon(aes(group=group,fill=zhibiao),colour="grey60")+
+ scale_fill_gradient(low="white",high="steelblue") +
+ coord_map("polyconic") +
+ geom_text(aes(x = jd,y = wd,label = province), data =province_city)+
+ theme(
+ panel.grid = element_blank(),
+ panel.background = element_blank(),
+ axis.text = element_blank(),
+ axis.ticks = element_blank(),
+ axis.title = element_blank()
+ )