Trying to combine multiple datasets into one bar graph in R

6.5k Views Asked by At

I'm trying to create a bar graph that has the same variables on the x-axis except separated by year. I want to show an increase/decrease over time but I'm not sure how to insert each dataset into the graph and have the years labelled. Each dataset is election results from a specific year.

Here's one dataset for example:

1992Election
Political Party 18-29,30-44,45-59,60+ (the numbered headings are age groups)
Democrat 0.40,0.37,0.40, 0.47 (voting percentages)
Republican 0.35,0.41,0.42, 0.47

For the first dataset I have the following code to create one bar graph:

1992Election

colnames(`1992Election`)[colnames(`1992Election`)=="X.18.29."] <- "18-29"
colnames(`1992Election`)[colnames(`1992Election`)=="X.30.44."] <- "30-44"
colnames(`1992Election`)[colnames(`1992Election`)=="X.45.59."] <- "45-59"
colnames(`1992Election`)[colnames(`1992Election`)=="X.60.."] <- "60+"

Party92 = rownames(`1992Election`)
n.group92 = nrow(`1992Election`)

color.names92 = c("royalblue2", "red2")

barplot(as.matrix(`1992Election`),beside = TRUE, xlab = "Age Group", 
        ylab = "Percent Voted", ylim = c(0,.7), col = color.names92)  
title(main = "Presidential Election Votes by Age in Ohio\n Year 1992")
legend("topright", title = "Political Party", Party92, fill = color.names92, 
  cex = 0.4)

this results in this graph: 1992Electiongraph

I would like to have the same variables along the x axis but grouped together by year

For ex: X- axis [the age groups (1992), [same age groups (1996), etc] if that makes sense, thanks.

2

There are 2 best solutions below

0
Emil Bode On

If the 2 datasets are similar, the simplest approach would be to bind your 2 datasets together. With rbind you get age groups together.

barplot(as.matrix(rbind(El92, El96)), beside=TRUE)

Or with cbind you get 8 groups of 2 bars, and if you want to change the order you can specify that:

barplot(as.matrix(cbind(El92, El96)[c(1,5,2,6,3,7,4,8)]), beside=TRUE)
0
Chris Ruehlemann On

Depending on what your data looks like and how it is structured, here's a simple solution using R base graphics:

Let's assume your data is roughly structured like this df, with one column for the years in which elections were held, and one column each for the results of the two parties:

y <- c(10, 14, 18, 22, 26) 
df <- data.frame(
  Year = c(paste("20",y, sep = "")),
  Rep = c(rnorm(5, 10)),
  Dem = c(rnorm(5, 15))
)

then you could save the election results in one vector, transposing them using tand cbinding them:

election.results <- t(cbind(df$Rep, df$Dem))

Now you could graph election.results in a barplot, using the besideargument and df$Yearfor the names in names.arg plus using legend to provide the key:

barplot(election.results, beside=T, names.arg = df$Year, col=c("red","blue"))
par(xpd=T) # this will probably not be necessary with the real data
legend(19, c("Republicans", "Democrats"), horiz = T, col=c("red","blue"), fill=c("red","blue"))

enter image description here