1

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.

Jess
  • 11
  • 1
  • 3
  • 1
    Please provide a MWE (minimal working example, or a subset of your data in 'copy-paste'able format, not images. Welcome to SO, read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), about how to format your question so that it's easier for others to help you! – massisenergy Dec 10 '18 at 18:07
  • 1
    I edited it to try and make it more helpful, thanks! – Jess Dec 10 '18 at 18:23
  • 2
    Thanks @[Jess](https://stackoverflow.com/users/10768686/jess) You can check this meanwhile, [how to use `dput()`](https://stackoverflow.com/questions/49994249/example-of-using-dput), while I make it more readable. – massisenergy Dec 10 '18 at 18:27
  • please have a read how to create an MCVE https://stackoverflow.com/help/mcve and how to ask a good question. https://stackoverflow.com/help/how-to-ask Please don't post images of data frames, but post either fake data or the dput output as suggested by @massisenergy – tjebo Dec 10 '18 at 19:07
  • now to your question, as suggested below, it is very important to structure your data first before plotting, that's probably more than 90 percent of the work. Make it into a long format, and use the year as grouping variable/ – tjebo Dec 10 '18 at 19:08

2 Answers2

2

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

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
0

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)
Emil Bode
  • 1,784
  • 8
  • 16