Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State level unemployment in R

Tags:

r

ggplot2

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.

like image 776
user227290 Avatar asked Mar 22 '11 01:03

user227290


People also ask

How is unemployment level calculated?

In general, the unemployment rate in the United States is obtained by dividing the number of unemployed persons by the number of persons in the labor force (employed or unemployed) and multiplying that figure by 100.

What is the steady state level of unemployment?

The steady state rate of unemployment, also called the natural rate of unemployment, is the average unemployment rate around which the economy fluctuates. The labor market is said to be in a steady state, or long-run equilibrium, when the rate of unemployment is constant.

Which states currently have the lowest unemployment rate?

Unemployment Minnesota had the lowest jobless rate in August, 1.9 percent. The next lowest rates were in New Hampshire and Utah, 2.0 percent each. The rate in Louisiana, 3.5 percent, set a new series low (all state series begin in 1976).


2 Answers

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
like image 167
Ramnath Avatar answered Oct 26 '22 18:10

Ramnath


Ramnath nailed this one. If you're still looking for other solutions, there's a decent example using other packages at the SAS-and-R blog.

like image 40
Ari B. Friedman Avatar answered Oct 26 '22 20:10

Ari B. Friedman