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)
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:
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)
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.)
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
#a simple reshaping, transposing our data
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: