使用R语言绘制芝加哥矢量图

昨日,一个在南大学城市规划的同学在新浪微博上@我



按照他的意思是把Json数据转换为矢量图,我之前并没有听过好这么转换的,上网站看了一下,发现芝加哥真的把城市规划的数据放在了github上面,具体见这里 :http://thechangelog.com/the-city-of-chicago-is-on-github/

随意选择其中的一个,我这里选择的是Bike routes in Chicago,就能看到github上面的项目,包括数据和例子这些。在ReadME.md中惊奇的发现居然也可以用R语言来绘制,正好要学R语言,于是小试了一下。值得一提的是,作者在文档中已经详细写了操作步骤,按照步骤一步一步来做即可。

# TITLE: Importing GeoJSON Example in R
# AUTHOR: Tom Schenk Jr., City of Chicago
# CREATED: 2013-01-23
# UPDATED: 2013-01-31
# NOTES:
# LIBRARIES: rgdal, ggplot2

# Set working directory (e.g., "C:\\Users\\username\\downloads" or "~/downloads")
setwd("path\\to\\folder")

# Install and load libraries
## If you need to install the RGDAL and GGPLOT2 libraries, complete this step first, otherwise, skip:
install.packages(c("rgdal", "ggplot2"))

library(rgdal)	# Import data into a Spatial Data Frame in R
library(ggplot2)	# Transform data from Shapefile to Data Frame

# Import data to Spatial Dataframe	
ogrInfo("data\\Bikeroutes.json", layer="OGRGeoJSON")

bikeroutes.shapefile <- readOGR(dsn="data\\Bikeroutes.json", layer="OGRGeoJSON", p4s="+proj=tmerc +ellps=WGS84") # Imports data. Replace PATH\\TO with actual file path (e.g., C:\\Users\\username\\downloads)

head(bikeroutes.shapefile) # Inspect the data structure.

plot(bikeroutes.shapefile) # Test plot of spatial data frame.

# Fortify data to translate to Data Frame
bikeroutes.df <- fortify(bikeroutes.shapefile) # Caution, this is very memory intensive and may take several hours to complete

head(bikeroutes.df) # Inspect the data structure

ggplot(bikeroutes.df, aes(x=long, y=lat, group=group)) + geom_path() # Test plot of data frame.

数据在github项目的 data文件夹下,csv数据下载下来即可,最终得到的效果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值