-1

Possible Duplicate:
How to plot two histograms together in R?

I want to plot two histograms together, both of them having same x axis units and y axis units. Two histograms are taken from two files, inp1 and inp2. I have tried the following code but it is not working:

x1<-hist(inp1, 120, plot = 0)  
x2<-hist(inp2, 120, plot = 0)  
hist(x1, x2, 240, plot = 1)
Community
  • 1
  • 1
newbie555
  • 467
  • 3
  • 6
  • 12
  • Do you want to plot them overlaid on top of one another, or in separate panels? – Josh O'Brien Jun 25 '12 at 17:32
  • 2
    @rockluke Then why don't you add some more explanation into what you want the plot to actually look like. – Dason Jun 25 '12 at 17:36
  • 2
    Some very similar questions that I discovered when searching for `[r] histograms`: http://stackoverflow.com/q/6957549/602276, http://stackoverflow.com/q/8901330/602276; http://stackoverflow.com/q/6200088/602276; http://stackoverflow.com/q/3541713/602276 – Andrie Jun 25 '12 at 17:40
  • I wanted to draw each bar side by side(not frequency, but absolute values) – newbie555 Jun 25 '12 at 17:50
  • 1
    @rockluke -- You might want to edit your question again to add those details (along with some simple example data). – Josh O'Brien Jun 25 '12 at 18:11

1 Answers1

9

The kind of plot you want is not strictly speaking a histogram. You can create something like it, though, using barplot() with beside=TRUE:

## Example data
d1 <- rnorm(1000)
d2 <- rnorm(1000, mean=1)

## Prepare data for input to barplot
breaks <- pretty(range(c(d1, d2)), n=20)
D1 <- hist(d1, breaks=breaks, plot=FALSE)$counts
D2 <- hist(d2, breaks=breaks, plot=FALSE)$counts
dat <- rbind(D1, D2)
colnames(dat) <- paste(breaks[-length(breaks)], breaks[-1], sep="-")

## Plot it
barplot(dat, beside=TRUE, space=c(0, 0.1), las=2)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455