0

I'm trying to plot 2 stacked bars over time. Essentially they would be like grouped bars over time (beside=TRUE), but also be stacked. Other stackoverflow questions answer similar issues, such as stacked and grouped charts, but don't work here - though please let me kow if you've seen a good example I've missed.

My strategy has been to plot the first set of bars, create space between them and try to plot the second in those spaces with whether par(new=TRUE) or add = TRUE argument in barplot. However, the second set of bars always overlap the first. Barplot documentation suggests that the offset argument should be useful, but I can't seem to find any examples using it and my own experimentation never seems to come out as expected.

Here is an example of code I've tried so far:

data1  = cbind(c(1,1.25),c(1.2,1.5),c(.75,1.2))
data2  = cbind(c(1.3,1.5),c(1,1.25),c(1.25,.75))

barplot(data1,
        space = 3,
        col = c(2,3))
barplot(data2,
        space = 3,
        col = c(4,5),
        add = TRUE)

Any suggestions or resources would be greatly appreciated.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
DaedalusBloom
  • 377
  • 5
  • 12
  • Since the answer seems to be using 'space' in `barplot` and there are 168 hits to a SO search on : [r] barlplot space, I would ask if you did any serching. (I am really, really tired of barplot questions.) – IRTFM Sep 06 '14 at 22:00
  • @BondedDust I have looked through them, but while some are certainly similar I wasn't able to find something that I was able to use in my situation, though I certainly could have overlooked something. Additionally at the time I thought the solution was more likely to come from the offset argument which has little documentation and few examples. I searched various things for about 2 hours before posting my question, so I made a genuine effort to not be redundant - though again I concede I may have overlooked something. – DaedalusBloom Sep 07 '14 at 16:29

1 Answers1

2

You can adjust the space parameter of the second plot. In this case, the space before the first bar needs to be larger than for the first plot. The spaces between bars, however, need to be the same. You can use the argument space = c(4, 3, 3) for the second plot.

barplot(data1,
        space = 3,
        col = c(2, 3))
barplot(data2,
        space = c(4, 3, 3),
        col = c(4, 5),
        add = TRUE)

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168