Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

A plot of ‘Who works at home’

$
0
0

By Wingfeet

I ran across this post containing displays on who works from home. I must say it looks great and is interactive but it did not help me understand the data. So I created this post to display the same data with a boring plot which might help me. For those really interested in this topic, census.gov created a .pdf which contains a full report with much more information than here.

Data

Data is from census.gov. I have taken the first spreadsheet. It is one of those spreadsheets with counts and percentages and empty lines to display categories. Very nice to check some numbers, horrible to process. So, a bit of code to extract the numbers.
library(gdata)
r1
# throw out percentages
r2
# put all column names in one row
r2$X.6[2]
r2$X.8[2]
# select part with data
r3
names(r3)[1]
r4
#eliminate one row with mean income.
r4
#reshape in long form
r5
varying=list(names(r4)[-1]),
v.names=’count’,
direction=’long’,
idvar=’Characteristic’,
timevar=’class’,
times=r3[1,2:4])
row.names(r5)
# remove ‘,’ from numbers and make numerical values.
# units are in 1000, so update that too
r5$count
# clean up numbers used for footnotes
r5$class
#some upfront ‘.’ removed.
r5$Characteristic
# create a factor
r5$Characteristic
levels=rev(r5$Characteristic[r5$class==’Home Workers’]))
# and create a higher level factor
r5$Mchar=r5$Characteristic
for (i in 1:nrow(r5)) r5$Mchar[i]
if(is.na(r5$count[i]) | r5$Mchar[i]==’Total’) r5$Mchar[i]
else r5$Mchar[i-1]

Plot

The plot is made using old style graphics. I could not get either ggplot2 or lattice to provide the plot I wanted.
# prepare for axis labels
index
index$y=56:1
index2
index3

r6
r6$class
par(mar=c(5,18,4,2)+.1,cex=.7)
plot(x=r6$count,y=r6$y,axes=FALSE,
xlab=’Count’,
ylab=”,
col=c(‘red’,’green’,’blue’)[r6$class],
frame.plot=TRUE,
# log=’x’,
ylim=c(2,58))
axis(1)
axis(2,at=index2$y,labels=index2$Characteristic,las=1)
text(y=index3$y-.1,x=30000,labels=index3$Characteristic,adj=0)
legend(‘topleft’,legend=levels(r6$class),
ncol=3,col=c(‘red’,’green’,’blue’),
border=NULL,pch=1,
yjust=0)

Why I did not use ggplot2?

The …read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles