Here’s a (still getting tweaked) R tutorial for the charts in the last post. Here’s the data you’ll need to download.
Set your working directory to wherever you want to work out of (usually a project folder)
setwd("/Users/pathToMyFolder...")
Next, load the data. Any format is fine, but our data is a tab-delimited .txt files, so we can use read.delim (here, my data is in a folder called “data,” but yours can be wherever).
data <- read.delim("data/states-data.txt")
Make a new field in your data frame that is the sum of unified states:
data$total.unified <- data$Unified.D+data$Unified.R
Now our data is ready to chart. It’s just one line of code to make a plot of the number of unified states over time, with “Year” on the x axis and “total.unified” on the Y axis:
plot(data$Year,data$total.unified,type='l',ylim=c(0,50))

The same plot, with extra arguments to clean it up a little:
plot(data$Year,data$total.unified,type='l',ylim=c(0,50),xlab="Year",ylab="States",main="States with unified control of state government since 1938",col="red",lwd=3) abline(h=c(0,10,20,30,40,50),col='lightgrey') abline(v=c(1940,1960,1980,2000),col='lightgrey')

Adding more layers onto the plot, drawing lines for Democratic- and Republican- unified states. (In general, “plot” makes a chart and “lines” add to an existing plot.)
plot(data$Year,data$Divided,type='l',ylim=c(0,30)) lines(data$Year,data$Unified.R,col="red") lines(data$Year,data$Unified.D,col="blue")

Now we’ll make a barplot instead. The syntax here is a little weird, and I had to get Amanda to fix mine originally, but it’s not so bad. Basically, our data needs to be transposed and reduced to just the columns we want to plot. You can do this in one step, but for clarity I’ll break it up here. It looks like a waffle chart just because of the horizontal axis lines, but it’s just a barplot.
#just the numbers we want to plot data.we.need<-data[,c("Unified.D","Divided","Unified.R")] #a simple reshaping, transposing our data transposed<-t(data.we.need) barplot(transposed,ylim=c(0,50),col=c('blue','grey','red'),border=F) abline(h=c(1:50),col='white')

We end up doing the same plot for the final output; it’s just shaped differently and has fewer axis lines. We’re also saving it as a pdf in the dimensions we want:
pdf(file="stacked-bars.pdf",width=8,height=5) barplot(transposed,ylim=c(0,50),col=c('blue','grey','red')) abline(h=c(10,20,30,40),col='white') dev.off()

Ship that!
-
dleanneb likes this
-
bleything likes this
-
drewgriffith15 likes this
-
mobileandwebdesignbunch likes this
-
learnfrominfo reblogged this from chartsnthings and added:
From simple charts to the NYT.
-
learnfrominfo likes this
-
edomaniac reblogged this from chartsnthings
-
janahang likes this
-
kcsluis reblogged this from chartsnthings
-
onlyandrewn likes this
-
crosas13 likes this
-
babydatajournalism reblogged this from jour72312
-
jour72312 reblogged this from chartsnthings and added:
I’ll keep saying this, I bet:...fine tuning visual encoding.
-
lurino likes this
-
immaculatellamalord reblogged this from onaissues
-
immaculatellamalord likes this
-
newsfastforward likes this
-
colorfulphraseology likes this
-
fromstarstostarfish likes this
-
onaissues reblogged this from chartsnthings and added:
Kevin Quealy, graphics editor at...New York Times, gave
-
marcoscan likes this
-
ellenalexia likes this
-
disleksili likes this
-
randomguywholikesstuff likes this
-
anomos likes this
-
ckalima likes this
-
dannydb likes this
-
pnnnk likes this
-
lifeandcode reblogged this from chartsnthings and added:
YESSSSSSSSS
-
coldbrain likes this
-
rumpl likes this
-
blairtheblur likes this
-
blairtheblur reblogged this from chartsnthings and added:
don’t have enough things...do, I’m trying
-
greglinch likes this
-
rolandcrosby likes this
-
panchocantu likes this
-
howardc-deux likes this
-
pseudoplacebo likes this
-
trieu likes this
-
bobandrsn likes this
-
fmadmonk likes this
-
tompearson likes this
-
chartsnthings posted this