Eka
Eka

Reputation: 15000

How to plot multiple histograms without overlapping in R

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

enter image description here

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

Upvotes: 2

Views: 11323

Answers (2)

comendeiro
comendeiro

Reputation: 836

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)
}

Upvotes: 11

Konrad
Konrad

Reputation: 18585

Or if you are interested in making use of dplyr / tidyr and ggplot2 you can use the code below. The idea is to gather the variables you want to visualise and then pass the generated categories to facets in ggplot2. This will give you a lot of flexibility in terms of arranging your charts by making use of facet_grid / facet_wrap and basic data.frame transformations. Personally, I find dplyr / ggplot2 combination very powerful and pleasant to read when working with a longer workflow.

Code

# Libs and data
Vectorize(require)(package = c("ggplot2", "ggthemes", "tidyr", "dplyr",
                               "xts"),
                                     character.only = TRUE)
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]

# Chart and data transformations
df %>%
    # Reshape
    gather(key = indicator, value = val) %>%
    # Basic chart
    ggplot(aes(x = val)) +
    geom_histogram(colour = "darkgreen", fill = "gray") +
    facet_wrap(~indicator, nrow = 2) +
    ## Theme and looks 
    theme_economist() +
    ggtitle("Histograms") +
    theme(strip.background = element_rect(fill = "gray80", colour = "black",
                                          size = 0.5, linetype = "solid"),
          strip.text = element_text(face = "bold"))

Preview

Facets: Histogram

Upvotes: 3

Related Questions