Donbeo
Donbeo

Reputation: 17617

R Overlap plot and histogram

I need to plot an histograms and a graph in the same plot. I am having problem with ggplot2 becouse the dataset is very big.

What can I do?

Here an example

lambda=seq(0,1,length.out=100)
b1=lambda^2
b2=lambda^2+1 
b=cbind(b1,b2)
perc=rnorm(100)
matplot(lambda,b)
hist(perc)

Thanks for help :D

Sorry my question was not very clear. I need to have the b and the histogram overlapped in the same plot. Something like the plot in this slide. enter image description here

This time i can not use ggplot becouse the dataset is too big and it takes to much times.

Upvotes: 0

Views: 627

Answers (2)

Greg Snow
Greg Snow

Reputation: 49640

Another option is to use the subplot command from the TeachingDemos package to add a new plot into an existing plot.

Upvotes: 0

ricardo
ricardo

Reputation: 8425

You are not (yet) using ggplot2, and if you do you will need other commands to control layout, but what i think you want (for base graphics) is the par command.

lambda=seq(0,1,length.out=100)
b1=lambda^2
b2=lambda^2+1 
b=cbind(b1,b2)
perc=rnorm(100)
par(mfrow = c(2,1))
matplot(lambda,b)
hist(perc)

This yields the matplot as the top chart, and the hist as the second chart.

If you want side-by-side, use par(mfrow = c(1,2)).

As noted in the comments, if you want them on top of each other call `par(new = TRUE) in between the plot commands as follows:

matplot(lambda,b)
par(new = TRUE)
hist(perc)

Upvotes: 2

Related Questions