Can you tell me without installing additional libraries is there a way to plot dynamically multiple histograms in R without over laping. Dynamically in the sense plotting histograms with change in number of columns. Below code only have 4 columns but can change between 2 and 20 columns.
example plot

My code
set.seed(3)
Ex <- xts(1:100, Sys.Date()+1:100)
df = data.frame(Ex,matrix(rnorm(100*4,mean=123,sd=3), nrow=100))
df<-df[,-1]
for(i in names(df)){
dfh<-hist(df[[i]], plot=FALSE)
}
plot(dfh,main="Histogram",xlab="x",col="green",label=TRUE)
This only plots the last histogram
If you want to have multiple plots in the same screen you can use the command
par(mfrow = c(2,1))
Where c(2,1) means you would like to have 2 rows and 1 column of charts, putting your charts side by side. If you put c(1,3) you would be telling R to put your charts in 1 row and 3 columns, and so on and so forth.
Then just plot your charts one after the other and they will fill the correspondent space.
EDIT: if you want to calculate automatically the row and columns for the par function you can create a function like this (or something more refined and pass it to par)
dimension = function(df){
kk = dim(df)[2];
x = round(sqrt(kk),0);
y = ceiling(kk/x);
return(c(x,y))
}
Being your code
set.seed(3)
Ex <- xts(1:100, Sys.Date()+1:100)
df = data.frame(Ex,matrix(rnorm(100*4,mean=123,sd=3), nrow=100))
df<-df[,-1]
par(mfrow = dimension(df))
for(i in names(df)){
hist(df[[i]] ,main="Histogram",xlab="x",col="green",label=TRUE,plot = TRUE)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With