Mlock Mlock -4 years ago 68
R Question

Combining Stacked and Side-by-side bar charts

I'm creating 2 bar graphs, the first whose height is a sum of 3 values that I'd like to show with separate colors. I can create that plot as shown:

library(reshape)
library(ggplot2)
data1 <- data.frame("Year" = c(2011, 2012, 2013, 2014, 2015, 2016),
"Item1" = c(47.3, 46.3, 49.9, 50.1, 50.9, 47.2),
"Item2" = c(26.9, 58.9, 2.0, 11.5, 11.5, 5.3),
"Item3" = c(25.8, 24.9, 24.9, 25.4, 25.3, 25.6))
mdata <- melt(data1, id.var = "Year")
plot(ggplot(mdata, aes(x = Year, y = value, fill = variable)) +
geom_bar(stat = "identity"))


My second plot is easier, just a single bar whose value determines the height of the bar. Code to generate that plot is:

data2 <- data.frame("Year" = c(2011, 2012, 2013, 2014, 2015, 2016),
"Value" = c(85.1, 83.4, 86.9, 87.6, 88.4, 85.6))
plot(ggplot(data2, aes(x = Year, y = Value)) +
geom_bar(stat = "identity"))


I would like to show these two bars side by side in the same plot, so that 2011 is grouped, 2012 is grouped, etc. I can't figure out how to add the second plot. Any help? Thanks!

Answer Source

Thanks for the feedback everyone. Based on the answers, I chose to change my approach slightly and used a line plot instead of the second bar plot. This communicates the same information, just not quite what I had originally wanted. Thanks again!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download