R Data Visualisation Example using R Base function

Create Demo dataset to be used for a plot.

set.seed(007)

x <- c(rnorm(300, mean=55, sd=5.5),
       rnorm(300, mean=65, sd=6.5))

head(x)
## [1] 67.57986 48.41776 51.18139 52.73239 49.66130 49.78996

Basic plots

A histogram can be created using the function hist(), which simplified format is as follow:

hist(x, breaks = "Sturges")

hist(x, col = "steelblue")

# Change the number of breaks
hist(x, col = "steelblue", breaks = 60)

Create density plots: density()

The function density() is used to estimate kernel density.

# Compute the density data
dens <- density(mtcars$mpg)

# plot density
plot(dens, frame = FALSE, col = "steelblue", 
     main = "Density plot of mpg") 

# Fill the density plot using polygon()
plot(dens, frame = FALSE, col = "steelblue", main = "Density plot of mpg") 
polygon(dens, col = "steelblue")