0

I would like to combine two ggplot barchart next to each other into one graph:

ggplot(data, aes(Year,lossratio1)) + geom_bar(stat="identity", colour="blue", fill="blue") 

ggplot(data, aes(Year, lossratio2) + geom_bar(stat="identity", colour="red", fill="red")

Note:

  • x-axis is the year
  • y-axis is lossratio1 and lossratio2.

Barchart fill follows the lossratio respectively.

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
  • 7
    Please show some data, eg. using `dput(head(data, 10))`. But I think there is already an answer, see this post for instance: http://stackoverflow.com/questions/18158461/grouped-bar-plot-in-ggplot – Roman Jul 28 '16 at 07:38
  • Year=c(2010,2011,2012,2013,2014,2015,2016) lossratio1=c(0.5,0.6,0.7,0.8,0.9,0.95,1) lossratio2=(0.6,0.65,0.7,0.75,0.8,0.85,0.9) data=data.frame(Year,lossratio1,lossratio2) The plot when I use this: ggplot(data)+geom_bar(aes(Year,lossratio1),data,stat="identity",colour="blue",fill="blue)+geom_bar(aes(Year,lossratio2),data,stat="identity",colour="red",fill="red"). This gives plot where the overlapping areas are in red. Where should I put the position="dodge"? – user6648245 Jul 28 '16 at 09:00

1 Answers1

0

You need to melt your data first, then you can set position = "dodge", like so:

library(reshape)
data.m <- melt(data, id.vars='Year')

ggplot(data.m, aes(Year, value)) + geom_bar(aes(fill = variable), position = "dodge", stat="identity")