0

I have created the below bar chart in Renter image description here

I have the average bar plot of High Risk, Med Risk and Low Risk. enter image description here

I now want to superimpose the 1st graph on the second, meaning inside the Second Graph, I want the bars under High Risk shown in first Graph to be put under the High Risk bar in the Second Graph. Likewise, for Med Risk and Low Risk. Can this be done in R?

user1946217
  • 1,733
  • 6
  • 31
  • 40
  • [Overlay Bar graphs in ggplot2][1] would be helpful [1]: http://stackoverflow.com/questions/13035295/overlay-bar-graphs-in-ggplot2 – Kumar Oct 23 '13 at 07:29
  • This is a waste of a giant geom (data ink ratio) and makes the visual less understandable (i.e. it would distract from the data). The results would be more interptable if you used a mean line per group laid over the first graph. What you suggest is likely doable but it's not the best visualization approach here. – Tyler Rinker Oct 23 '13 at 14:08

1 Answers1

1

As Tyler pointed out, what you want isn't a good idea, but to answer your question, here is some code that should get you started:

##Generate some data
heights = runif(15)
heights = heights/sum(heights)
dd = data.frame(heights, type=1:3)
m_heights = tapply(dd$heights, dd$type, mean)

The trick is to generate empty bars when adding your second barplot:

##rep(0, 5) is used to pad
h = c(rep(0,5), heights[1:5], rep(0,5), heights[6:10], rep(0,5), heights[11:15])
barplot(m_heights, width=1, space=1,  ylim=c(0, max(dd$heights)), xlim=c(0, 6))
barplot(h, width=0.2,space=0,add=T,  col="white", border=NULL)

However, a much better plot is just a plain scatter plot of the data

plot(dd$type, dd$heights)

We can even add on the means:

points(1:3, m_heights, col=2, pch="X")
csgillespie
  • 59,189
  • 14
  • 150
  • 185