楼主: oliyiyi
1593 6

Data Visualization of Census Data with R [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


This article shows step-by-step how to use R to access US Census Data, visualize it, and plot it on the map.

By Krishna Prasad, June 2014.

The article mainly focuses on how to use R to access and visualize census data.  There are contributed packages that greatly enhance your ability to interact with the graphs you create in R. I will mainly focus on obtaining data from the US Census via an API connection and plotting data on different types of US maps.

Our first step is figuring out how to use the Census API within R.

Then we use the acs.lookup function to find the required data in all tables using key words.

For example, the following are the search results for the keywords owner, occupied, and median.

>acs.lookup(endyear=2012, span=5,dataset="acs", keyword= c("owner", "occupied", "median"), case.sensitive=F)

An object of class "acs.lookup"

endyear= 2012  ; span= 5

results:

variable.code           table.number                         table.name
1    B25021_002       B25021               MEDIAN NUMBER OF ROOMS BY TENURE
2    B25037_002       B25037               MEDIAN YEAR STRUCTURE BUILT BY TENURE
3    B25039_002       B25039               MEDIAN YEAR HOUSEHOLDER MOVED INTO UNIT BY TENURE
4    B25119_002       B25119               Median Household Income by Tenure

variable.name
1               Median number of rooms -- Owner occupied
2               Median year structure built -- Owner occupied
3                Median year householder moved into unit -- Owner occupied
4                Median household income in the past 12 months (in 2012 inflation-adjusted dollars) -- Owner occupied (dollars)

Visualizing Census Data on Maps

Using choroplethr simplifies the creation of choropleths (thematic maps) in R. It provides native support for creating choropleths from US Census data. This functionality is available with the choroplethr_acs function.

How it Works—

The choroplethr package does not store any data locally. Instead, it uses the R acs package to get ACS data via the Census API. This means a few things for users of choroplethr.

>library(acs)
>api.key.install(key=" your secret key here")
>choroplethr_acs("B01002", "state", endyear=2012, span=5)

Table B01002 has 3 columns.  Please choose the column to render:

1: Median Age by Sex: Median age -- Total:
2: Median Age by Sex: Median age -- Male
3: Median Age by Sex: Median age -- Female

Selection: 1


Fig. 1 US Census - Median Age of Home Buyers

According to the National Association of Home Builders (NAHB) study,the average buyer is expected to stay in a home for 13 years. To know the major cost paid by home buyers, we combined median home price data and average home insurance over a period of 13 years, and plotted the data on the US map to give a clear view of the total costs by state.

#Downloading median home price data
>my.states=geo.make(state="*")
>home_median _price<-acs.fetch(geography=my.states, table.number="B25077")
>write.csv(home_median _price, file=".home_median _price.csv")

Downloaded Average Latitude and Longitude for US States from MAX MIND

#mergingthree data frames average insurance and median home price
>Total_Cost<- merge (home_median _price,home_average_insurance,Lat_Long, by="State")
# adding median home price and 13 years average insurance
>Total_Cost$Sum<- Total_Cost $Median_Price+Total_Cost$Average_Insurance
# plottingdata on the US map
>install.packages("ggmap")
>install.packages("mapproj")
>library(ggmap)
>library(mapproj)<br< >map<- get_map(location = 'US', zoom = 4)
>ggmap(map)
> TC <- ggmap(map) + geom_point(aes(x = Longitude, y = Latitude, size = Total.Cost.in.USD), data = state_median_income, alpha = .5)+ ggtitle("Total Cost of Homes in the US")
> TC



Author Profile: Krishna Prasad is a Data Analyst with experience programming in Python and R. He is a Computer Science Engineer from JNTU, Hyderabad.






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Visual Census ATION Data With different obtaining article ability access

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
日新少年 + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

本帖被以下文库推荐

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
沙发
日新少年 学生认证  发表于 2016-7-3 12:23:57 |只看作者 |坛友微信交流群
谢谢楼主分享
已有 1 人评分论坛币 收起 理由
oliyiyi + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

使用道具

藤椅
auirzxp 学生认证  发表于 2016-7-3 12:37:27 |只看作者 |坛友微信交流群
谢谢楼主分享
已有 1 人评分论坛币 收起 理由
oliyiyi + 10 精彩帖子

总评分: 论坛币 + 10   查看全部评分

使用道具

板凳
bailihongchen 发表于 2016-7-4 13:01:17 |只看作者 |坛友微信交流群
thanks for sharing
已有 1 人评分论坛币 收起 理由
oliyiyi + 10 精彩帖子

总评分: 论坛币 + 10   查看全部评分

使用道具

报纸
tamtam7010 发表于 2016-7-18 22:36:59 |只看作者 |坛友微信交流群
thanks for your sharing on this topic that I did not learn before, thanks again
已有 1 人评分论坛币 收起 理由
oliyiyi + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

使用道具

地板
jiandong4388 学生认证  发表于 2016-7-18 23:30:30 来自手机 |只看作者 |坛友微信交流群
谢谢分享,学习了。
已有 1 人评分论坛币 收起 理由
oliyiyi + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

使用道具

7
peterlovejin 发表于 2023-4-23 00:13:00 |只看作者 |坛友微信交流群

thanks for sharing
已有 1 人评分论坛币 收起 理由
oliyiyi + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 02:18