2

I want to make a histogram for multiple variables. I used the following code :

set.seed(2)
dataOne <- runif(10)
dataTwo  <- runif(10)
dataThree <- runif(10)
one <-  hist(dataOne, plot=FALSE)
two  <-  hist(dataTwo, plot=FALSE)
three  <- hist(dataThree, plot=FALSE)
plot(one, xlab="Beta Values", ylab="Frequency",
     labels=TRUE, col="blue", xlim=c(0,1))
plot(two, col='green', add=TRUE)
plot(three, col='red', add=TRUE)

But the problem is that they cover each other, as shown below.

I just want them to be added to each other (showing the bars over each other) i.e. not overlapping/ not covering each other. How can I do this ?

enter image description here

dardisco
  • 5,086
  • 2
  • 39
  • 54
behroooz torabi
  • 21
  • 1
  • 1
  • 2
  • Hi and Welcome to stackoverflow! As you are new on SO, please take some time to read [about Stackoverflow](http://stackoverflow.com/about) and [how to ask](http://meta.stackoverflow.com/help/how-to-ask). You are much more likely to receive a helpful answer if you provide a [minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Please also clarify the desired output. Thanks! – Henrik Oct 09 '13 at 12:59

2 Answers2

0

Try replacing your last three lines by:

plot(One, xlab = "Beta Values", ylab = "Frequency", col = "blue")
points(Two, col = 'green')
points(Three, col = 'red')

The first time you need to call plot. But the next time you call plot it will start a new plot which means you lose the first data. Instead you want to add more data to it either with scatter chart using points, or with a line chart using lines.

Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136
0

It's not quite clear what you are looking for here.

One approach is to place the plots in separate plotting spaces:

par("mfcol"=c(3, 1))
hist(dataOne, col="blue")
hist(dataTwo, col="green")
hist(dataThree, col="red") 
par("mfcol"=c(1, 1))

Is this what you're after?

enter image description here

dardisco
  • 5,086
  • 2
  • 39
  • 54