Plotting Histograms and Gaussian Distribution curves in R

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 189
Posts: 5943
Joined: 10 years ago
Location: Tanzania
Contact:

#1

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 10385 times
Histograms.png
Histograms.png (9.15 KiB) Viewed 10385 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 10374 times
Gaussian_distr.png
Gaussian_distr.png (14.68 KiB) Viewed 10374 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 10339 times
Normalized_Gaussian_distr.png
Normalized_Gaussian_distr.png (14.78 KiB) Viewed 10339 times
1
1 Image
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 189
Posts: 5943
Joined: 10 years ago
Location: Tanzania
Contact:

#2

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

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 189
Posts: 5943
Joined: 10 years ago
Location: Tanzania
Contact:

#3

Outputs
Attachments
h1.jpeg
h1.jpeg (21.05 KiB) Viewed 9091 times
h1.jpeg
h1.jpeg (21.05 KiB) Viewed 9091 times
h2.jpeg
h2.jpeg (20.84 KiB) Viewed 9091 times
h2.jpeg
h2.jpeg (20.84 KiB) Viewed 9091 times
h3.jpeg
h3.jpeg (20.93 KiB) Viewed 9091 times
h3.jpeg
h3.jpeg (20.93 KiB) Viewed 9091 times
h4.jpeg
h4.jpeg (32.81 KiB) Viewed 9091 times
h4.jpeg
h4.jpeg (32.81 KiB) Viewed 9091 times
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “R”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest