Page 1 of 1

Plotting Histograms and Gaussian Distribution curves in R

Posted: Tue May 23, 2017 1:48 pm
by Eli
An approach to plot a histogram for a given dataset can be achieved in more or less the same way as previously done using Python here. In this post, we show using R how to plot a histogram and its corresponding Gaussian/Normal distribution from the given or a randomly generated data set.

  1. # Create example data
  2. x = sample(-100:100, 50)
  3.  
  4. #Normalized data
  5. normalized = (x-min(x))/(max(x)-min(x))
  6.  
  7. #Plot Histograms of example data and normalized data
  8. png(filename='Histograms.png')
  9. par(mfrow=c(1,2))
  10. hist(x,          breaks=10, xlab="Data",            col="lightblue", main="")
  11. hist(normalized, breaks=10, xlab="Normalized Data", col="lightblue", main="")
  12. dev.off()


Output

Histograms.png
Histograms.png (9.15 KiB) Viewed 1801 times
Histograms.png
Histograms.png (9.15 KiB) Viewed 1801 times

The corresponding Gaussian distribution for the data sample can be achieved as follows:

  1. x <- seq(-100, 100, length = 200)
  2. s = sd(x)
  3. mu = mean(x)
  4. y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2))
  5. png(filename='Gaussian_distr.png')
  6. plot(x,y, type="l", lwd=2, col = "red", xlim = c(-100,100))
  7. dev.off()


with the output:

Gaussian_distr.png
Gaussian_distr.png (14.68 KiB) Viewed 1790 times
Gaussian_distr.png
Gaussian_distr.png (14.68 KiB) Viewed 1790 times

We can however normalize the y-axis to have the data range from 0 to 1:

  1. x <- seq(-100, 100, length = 200)
  2. s = sd(x)
  3. mu = mean(x)
  4. y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2))
  5. Normalized = (y-min(y))/(max(y)-min(y))
  6. png(filename='Normalized_Gaussian_distr.png')
  7. plot(x, Normalized, type="l", lwd=2, col = "red", xlim = c(-100,100))
  8. dev.off()


Output

Normalized_Gaussian_distr.png
Normalized_Gaussian_distr.png (14.78 KiB) Viewed 1755 times
Normalized_Gaussian_distr.png
Normalized_Gaussian_distr.png (14.78 KiB) Viewed 1755 times

Re: Plotting Histograms and Gaussian Distribution curves in R

Posted: Sat Oct 29, 2022 8:01 pm
by Eli
Labelling update

  1. x <- sample(-100:100, 50) #our n = 50
  2. x = sample(-100:100, 50)
  3.  
  4. png(filename='Histograms.png')
  5. par(mfrow=c(1,1))
  6. hist(x, breaks=10, xlab="Data", col="lightblue", main="")
  7.  
  8. #Calling title() function
  9. title(main = "Created at \n www.tssfl.com", sub = " ",
  10.       xlab = " ", ylab = " ",
  11.       cex.main = 2,   font.main = 3, col.main = "darkgreen",
  12.       cex.sub = 2, font.sub = 3, col.sub = "darkgreen",
  13.       col.lab ="black"
  14.       )
  15. dev.off()


Re: Plotting Histograms and Gaussian Distribution curves in R

Posted: Sat Oct 29, 2022 8:22 pm
by Eli
Outputs