user8389133
user8389133

Reputation:

how to apply abline function to a list of plot in R

I have a list of plots using this code (the code is an answer for my last question.

the last question

xxxy <- as.data.frame(matrix(runif(1e3), ncol = 10))
names(xxxy) <- paste0('V', 1:10)

nam <- names(xxxy)



  par(mfrow = c(2, 5))
   out_par<- lapply(seq_along(nam), function(i)
            hist(xxxy[[i]],main = paste("Histogram of", nam[[i]]))
    )

I would like to add a line to each plot of the list. I tried this code:

 mean_par2 <- list(2.3,4.5,3.2,4.6,2,1.5,1.2,1.8,2.5,2)
 true_par2 <- list(2,4.2,3,4.5,2,1.2,1,1.79,2.55,1.89)
lapply(seq_along(out_par), function(i) abline(v=c(mean_par2[[i]],true_par2[[i]]),col=c("red","blue")))

The code worked only for the last plot! Where is my problem? Any help please?

Upvotes: 1

Views: 547

Answers (1)

CPak
CPak

Reputation: 13581

Plot the histogram and vertical lines immediately after each other

set.seed(1)
mean_par2 <- runif(10)*0.8
true_par2 <- runif(10)*0.8
par(mfrow = c(2, 5))
mapply(function(x, y, z) {hist(x); abline(v=c(y,z),col=c("red","blue"))}, xxxy, mean_par2, true_par2)

NOTE You had another issue in that your mean_par2 and true_par2 values were outside the range of your histogram values.

Upvotes: 1

Related Questions