1

Hi I am trying to create a beside barplot but my bars are not in the correct spaces appear to be overlapping.

barplot(counts, 
    main ="2011 vs 2016 Men Volunteering",
    ylim=c(0,320000),
    xlab="Age (years)",
    ylab = "Frequancy",
    names.arg = c("15-19", "20-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", "84 and over"),
    col=c("red","green"),
    beside=TRUE)

legend("topright", 
       legend = c("2011", "2016"), 
       fill = c("red", "green"))

Outputting this:

When in reality I want it to look like this:

I have tried using ncol = 2 but I can't get it to work, how do I implement it?

Like this,

barplot(counts, 
    main ="2011 vs 2016 Men Volunteering",
    ylim=c(0,320000),
    xlab="Age (years)",
    ylab = "Frequancy",
    names.arg = c("15-19", "20-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", "84 and over"),
    ncol = 2,
    col=c("red","green"),
    beside=TRUE)

legend("topright", 
       legend = c("2011", "2016"), 
       fill = c("red", "green"))

Or this?

counts <- as.matrix(ELEVENMenVolunteerAge, SIXTEENMenVolunteerAge, ncol = 2)

All help appreciated thank you so much.

Prradep
  • 5,506
  • 5
  • 43
  • 84
Kane Deck
  • 17
  • 3
  • 3
    Welcome to SO. To make it easier for others to help you, it is better to also include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Aug 20 '17 at 07:03
  • 1
    To be more specific, including the output of `dput(counts)` would improve your question a lot. – Jaap Aug 20 '17 at 07:09

1 Answers1

0

As there is no reproducible data for counts, I am including few examples on how dodged plots can be created.


ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
    geom_bar(position = "dodge")

enter image description here

# Grouped Bar Plot
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, col=c("darkblue","red"),
        legend = rownames(counts), beside=TRUE)

enter image description here

Prradep
  • 5,506
  • 5
  • 43
  • 84