Home > OS >  R adding new plots
R adding new plots

Time:11-09

This is my first question on the forum. Hope I don't give too much code in question.

My code (the part that works fine)

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


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

mean_x <- mean(x)
mean_x1 <- mean(x1)

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


corners <- par('usr') # get the corners of the plots


x_left <- corners[1]
x_right <- quantile(x,0.95) # at 95 percentile, change as needed

x1_left <- x_right
x1_right <- quantile(x1,0.05) # at 5th percentile, change if needed

rect(xleft=x_left,xright=x_right,ybottom=corners[3],ytop=corners[4],
     col = 'lightblue',density = 100)
rect(xleft=x1_left,xright=x1_right,ybottom=corners[3],ytop=corners[4],
     col = 'pink',density = 100)


plot(y, col='lightgray',add=TRUE)
plot(y1, col='gray48',add=TRUE)


abline(v=mean_x,col='forestgreen',lwd=3)
abline(v=mean_x1,col='forestgreen',lwd=3)


abline(v=x_right,col='steelblue',lwd=3)
abline(v=x1_right,col='firebrick',lwd=3)

I want new plots to appear below not on the charts already existing. I tried this code:

# I want to use par, let additional histograms appear below
par(mfrow=c(5,1), mar = c(2,0,2,0),oma = c(1,5,0,0))


# Function V (the result will then be taken as the mean for the histograms)
V <- function( C, H, n ){
  1 / 1   ( C / H )^n 
}

# with a C equal to 10
V_10 <- V(10,1,1)
x3 <- rnorm(1000,V_10,1)
y3 <- hist(x3, plot=FALSE,breaks=20)
plot(y3, col='gray48',add=TRUE)
mean_x3 <- mean(x3)



# with a C equal to 50
V_50 <- V(50,1,1)
x4 <- rnorm(1000,V_50,1)
y4 <- hist(x4, plot=FALSE,breaks=20)
plot(y4, col='gray48',add=TRUE)
mean_x4 <- mean(x4)

Unfortunately, the new plots do not appear below the top two. Therefore, I am asking for help on how to fix it.Since this is my first question on the forum, I am asking for your indulgence.

CodePudding user response:

If you want to repeat the blue and red shaded areas, you should put that into a function to avoid duplicating the code. I've done that in the example below. To get your plots to appear together in a single layout, you should specify par(mfrow=c(5,1...) at the top of the code. Because the base_plot_fun() I created here generates a new plot, you can keep add=TRUE in your overlaid histograms.

# I want to use par, let additional histograms appear below
par(mfrow=c(5,1), mar = c(2,0,2,0),oma = c(1,5,0,0))

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


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

base_plot_fun <- function(x, x1, y, y1) {
  mean_x <- mean(x)
  mean_x1 <- mean(x1)
  
  # initialize the plot
  plot(
    0,
    0,
    ylim = range(c(y$counts, y1$counts)),
    xlim = range(c(y$breaks, y1$breaks)),
    xlab = "x",
    ylab = "counts",
    type = "n"
  )
  
  
  corners <- par('usr') # get the corners of the plots
  
  x_left <- corners[1]
  x_right <- quantile(x, 0.95) # at 95 percentile, change as needed
  
  x1_left <- x_right
  x1_right <- quantile(x1, 0.05) # at 5th percentile, change if needed
  
  rect(
    xleft = x_left,
    xright = x_right,
    ybottom = corners[3],
    ytop = corners[4],
    col = 'lightblue',
    density = 100
  )
  rect(
    xleft = x1_left,
    xright = x1_right,
    ybottom = corners[3],
    ytop = corners[4],
    col = 'pink',
    density = 100
  )
}

base_plot_fun(x = x, x1 = x1, y = y, y1 = y1)

corners <- par('usr') # get the corners of the plots

mean_x <- mean(x)
mean_x1 <- mean(x1)

x_left <- corners[1]
x_right <- quantile(x, 0.95) # at 95 percentile, change as needed

x1_left <- x_right
x1_right <- quantile(x1, 0.05) # at 5th percentile, change if needed

plot(y, col='lightgray',add=TRUE)
plot(y1, col='gray48',add=TRUE)


abline(v=mean_x,col='forestgreen',lwd=3)
abline(v=mean_x1,col='forestgreen',lwd=3)


abline(v=x_right,col='steelblue',lwd=3)
abline(v=x1_right,col='firebrick',lwd=3)

# Function V (the result will then be taken as the mean for the histograms)
V <- function( C, H, n ){
  1 / 1   ( C / H )^n 
}

# with a C equal to 10
V_10 <- V(10,1,1)
x3 <- rnorm(1000,V_10,1)
y3 <- hist(x3, plot=FALSE,breaks=20)
base_plot_fun(x = x, x1 = x1, y = y, y1 = y1)
plot(y3, col='gray48',add=T)
mean_x3 <- mean(x3)
abline(v=mean_x3,col='forestgreen',lwd=3)


# with a C equal to 50
V_50 <- V(50,1,1)
x4 <- rnorm(1000,V_50,1)
y4 <- hist(x4, plot=FALSE,breaks=20)
base_plot_fun(x = x, x1 = x1, y = y, y1 = y1)
plot(y4, col='gray48',add=T)
mean_x4 <- mean(x4)
abline(v=mean_x4,col='forestgreen',lwd=3)

Created on 2022-11-09 with reprex v2.0.2

  • Related