Home » Histogram and Density plots

Histogram and Density plots

 

#loading data

data(iris)

Hitogram

hist(iris$Sepal.Length, main = "Histogram of Iris sepal length",
     col = "grey", xlab = "sepal length")

unnamed-chunk-21

hist(iris$Sepal.Length, main = "Histogram of Iris sepal length",
     col = "grey", xlab = "sepal length", breaks = 16 )

unnamed-chunk-22

hist(iris$Sepal.Length, main = "Histogram of Iris sepal length",
     col = "grey", xlab = "sepal length")

unnamed-chunk-23

hist(iris$Sepal.Width, main = "Histogram of Iris sepal width",
     col = "grey", xlab = "sepal length")

unnamed-chunk-24

hist(iris$Petal.Length, main = "Histogram of Iris petal length",
     col = "grey", xlab = "petal length")

unnamed-chunk-25

hist(iris$Petal.Width, main = "Histogram of Iris petal width",
     col = "grey", xlab = "petal width")

unnamed-chunk-26

Density plot

Sepal.LengthMean <- mean(iris$Sepal.Length)
Sepal.WidthMean <- mean(iris$Sepal.Width)
Petal.LengthMean <- mean(iris$Petal.Length)
Petal.WidthMean <- mean(iris$Petal.Width)

Sepal.LengthSD <- sd(iris$Sepal.Length)
Sepal.WidthSD <- sd(iris$Sepal.Width)
Petal.LengthSD <- sd(iris$Petal.Length)
Petal.WidthSD <- sd(iris$Petal.Width)

#density plot
hist(iris$Sepal.Length, 
     main = "Density plot of Iris sepal length",
     col = "grey", xlab = "sepal length", freq=FALSE,
     ylim=c(0,0.55))
curve(dnorm(x, mean=Sepal.LengthMean,
            sd= Sepal.LengthSD), add= TRUE, col="red", lwd =2)

unnamed-chunk-31

hist(iris$Sepal.Width,
     main = "Density plot of Iris sepal width",
     col = "grey", xlab = "sepal length", freq=FALSE)
curve(dnorm(x, mean=Sepal.WidthMean,
             sd=Sepal.WidthSD), add= TRUE, col="red", lwd=2)

unnamed-chunk-32

hist(iris$Petal.Length,
     main = "Density plot of Iris petal length",
     col = "grey", xlab = "petal length", freq=FALSE)
curve(dnorm(x, mean=Petal.LengthMean,
            sd=Petal.LengthSD ),add=TRUE, col="red", lwd=2)

unnamed-chunk-33

hist(iris$Petal.Width,
     main = "Density plot of Iris petal width",
     col = "grey", xlab = "petal width", freq = FALSE)
curve(dnorm(x, mean = Petal.WidthMean,
            sd=Petal.WidthSD), add=TRUE, col="red", lwd=2)

unnamed-chunk-34