Home > other >  R shift of the plots
R shift of the plots

Time:11-08

I have a code in R

x=rnorm(1000,1,1)
quantile(x,0.05)


x1=rnorm(1000,-10,1)

sum(x1>quantile(x,0.05))/length(x1)

y=hist(x,plot=FALSE)$density

plot(y)

plot(y,type="l")


y1=hist(x1,plot=FALSE)$density
matplot(y1,type="l",add=TRUE)

I want to change it so that the plots do not overlap but are next to each other. Is it enough that I change the values for the mean and sd or I have to change something else in the code. I am new to this, so please help me

CodePudding user response:

In order to plot both histograms, you need to set the correct x and y limits for the plot windows because base R graphics will not resize the window after the first set of data has been drawn. Here's one way to do that

x <- rnorm(1000,1,1)
x1 <- rnorm(1000,-10,1)

y <- hist(x,plot=FALSE)
y1 <- hist(x1, plot=FALSE)

plot(0,0, 
  ylim=range(c(y$counts, y1$counts)), 
  xlim=range(c(y$breaks, y1$breaks)), 
  xlab="x", ylab="counts", type="n")
plot(y, add=TRUE)
plot(y1, add=TRUE)

enter image description here

  • Related