- # Create example data
- x = sample(-100:100, 50)
- #Normalized data
- normalized = (x-min(x))/(max(x)-min(x))
- #Plot Histograms of example data and normalized data
- png(filename='Histograms.png')
- par(mfrow=c(1,2))
- hist(x, breaks=10, xlab="Data", col="lightblue", main="")
- hist(normalized, breaks=10, xlab="Normalized Data", col="lightblue", main="")
- dev.off()
Output
The corresponding Gaussian distribution for the data sample can be achieved as follows:
- x <- seq(-100, 100, length = 200)
- s = sd(x)
- mu = mean(x)
- y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2))
- png(filename='Gaussian_distr.png')
- plot(x,y, type="l", lwd=2, col = "red", xlim = c(-100,100))
- dev.off()
with the output:
We can however normalize the y-axis to have the data range from 0 to 1:
- x <- seq(-100, 100, length = 200)
- s = sd(x)
- mu = mean(x)
- y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2))
- Normalized = (y-min(y))/(max(y)-min(y))
- png(filename='Normalized_Gaussian_distr.png')
- plot(x, Normalized, type="l", lwd=2, col = "red", xlim = c(-100,100))
- dev.off()
Output