1

I have the following dataset:

  Year Generated Rejected
1 2012 133118208  7256986
2 2013 289487598 49652610
3 2014 192232775 31765480
4 2015  40434968  2513930

I am trying to generate a grouped bar chart that will have Year on the x axis and a numerical scale on the y axis to show the generated Vs rejected year-wise.

I am confused what I should set my y axis to? I have not tried using the spread() function yet, as I am hoping there might be an easier way

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
K3rn3l5
  • 351
  • 4
  • 18

1 Answers1

3

First you have to arrange the data in the proper format for plotting:

library(reshape2)
df1 <- melt(df, id = "Year")

Or you could use the tidyr package:

library(tidyr)
df1 <- gather(df, variable, value, -Year)

Which may be easier to understand if you are not familiar with melt()

Basically, it says: "Gather all the variables in df except Year, calling the new key column variable and the new value column value"

library(ggplot2)
ggplot(df1, aes(Year, value)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat = "identity")

enter image description here

If you prefer to format numbers with commas separating thousands:

library(ggplot2)
library(scales)
ggplot(df1, aes(Year, value)) +   
  geom_bar(aes(fill = variable), position = "dodge", stat = "identity") +
  scale_y_continuous(name = "Values", labels = comma)

enter image description here

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
  • 2
    Beaupre: a good answer. OP can also pick colors and change the legend title, among many other choices: ggplot(df1, aes(Year, value)) + geom_bar(aes(fill = variable), position = "dodge", stat = "identity") + scale_fill_manual(values = c("blue", "forestgreen")) + labs(fill = "Legend") – lawyeR Mar 07 '15 at 00:57
  • Thanks, that did the trick. Though I was hoping to avoid reshaping the data, but guess that was the only way. – K3rn3l5 Mar 07 '15 at 02:17