R级国家级失业率 [英] State level unemployment in R

查看:122
本文介绍了R级国家级失业率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个新手问题。我想绘制美国地图上的州级失业率。关于如何计划县级失业及其相关问题,在这里和其他地方进行了深刻的讨论。代码看起来让我感到害怕。是否有一个简单的代码需要两列,一个状态代码和一个因子变量指示数字间隔,并产生彩色美国地图(基于因子变量)。另一个补充问题是,如果我需要在美国主要城市进一步创造类似的情节,但需要失业率,我该如何修改代码。
预先感谢您。

This is a newbie question. I want to plot the state level unemployment in the US map. There have been profound discussions here and elsewhere about how to plot county level unemployment and the issues associated with it. The code looks intimidating to me. Is there a simple code out there which takes two columns, a state code and a factor variable indicating numeric intervals and yields a colored US map(based on the factor variable). A supplementary question is that if I need to go a little further and create similar plot but with unemployment rate in major cities of US how do I modify the code. Thank you in advance.

推荐答案

这里有一段代码,带有解释每一步的注释。如果您有任何疑问,请告知我们。

Here is a quick piece of code with comments explaining each step. Let me know if you have questions

# load libraries
library(XML);
library(ggplot2);
library(maps);
library(plyr);

# read the data from the bls website with correct column formats
unemp = readHTMLTable('http://www.bls.gov/web/laus/laumstrk.htm',
  colClasses = c('character', 'character', 'numeric'))[[2]];

# rename columns and convert region to lowercase
names(unemp) = c('rank', 'region', 'rate');
unemp$region  = tolower(unemp$region);

# get us state map data and merge with unemp
us_state_map = map_data('state');
map_data = merge(unemp, us_state_map, by = 'region'); 

# keep data sorted by polygon order
map_data = arrange(map_data, order);

# plot map using ggplot2

p0 = ggplot(map_data, aes(x = long, y = lat, group = group)) +
     geom_polygon(aes(fill = cut_number(rate, 5))) +
     geom_path(colour = 'gray', linestyle = 2) +
     scale_fill_brewer('Unemployment Rate (Jan 2011)', pal = 'PuRd') +
     coord_map();
#You may need to spell out the argument pal as pallete

这篇关于R级国家级失业率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆