Eka
Eka

Reputation: 15000

Multiple histogram with overlay standard deviation curve in R

I want to plot a histogram similar to this in R (if possible without installing additional libraries).

enter image description here

The image should contain a histogram, label indicating frequency, a standard deviation curve, mean line and lines indicating distance of standard deviation eg a red line at +1, -1 SD, yellow line at +2,-2 SD and a green line at +3,-3 SD

This is the code for plotting multiple histogram but its unable to plot the standard deviation curve. The code for piloting standard deviation is taken from here.

library(xts)
dimension = function(df){
  kk = dim(df)[2];

  x = round(sqrt(kk),0);
  y = ceiling(kk/x);

  return(c(x,y))
}


set.seed(3)
Ex <- xts(1:100, Sys.Date()+1:100)
df = data.frame(Ex,matrix(rnorm(100*6,mean=123,sd=3), nrow=100))
df<-df[,-1]
m<-list()
std<-list()

par(mfrow = dimension(df))

for(i in names(df)){
  m[[i]]<-mean(df[[i]],na.rm=TRUE)
  std[[i]]<-sd(df[[i]],na.rm=TRUE)
  hist(df[[i]] , main="Histogram",xlab="x",col="green",label=TRUE,plot = TRUE)  
  curve(dnorm(x, mean=m[[i]], sd=std[[i]]), col="darkblue", lwd=2, add=TRUE, yaxt="n")
}

Upvotes: 2

Views: 7419

Answers (1)

royr2
royr2

Reputation: 2289

So the standard deviation lines can be easily added using abline(). Also, as suggested by Pascal use freq = FALSE to scale the y-axis appropriately.

for(i in names(df)){
  m[[i]]<-mean(df[[i]],na.rm=TRUE)
  std[[i]]<-sd(df[[i]],na.rm=TRUE)
  hist(df[[i]] , main="Histogram",xlab="x",col="green",label=TRUE,plot = TRUE, freq = F)  
  curve(dnorm(x, mean=m[[i]], sd=std[[i]]), col="darkblue", lwd=2, add=TRUE, yaxt="n")

  # Use abline 
  abline(v = m[[i]], lty = 2) 
  abline(v = m[[i]]+std[[i]], lty = 2)
  abline(v = m[[i]]-std[[i]], lty = 2)
  abline(v = m[[i]]+2*std[[i]], lty = 2)
  abline(v = m[[i]]-2*std[[i]], lty = 2)
}

Upvotes: 2

Related Questions