Visualizing distributions through a kernel-density plot

The kernel-density plot is another method of visualizing the distribution of numeric variables. In this recipe, we will see how we can produce a kernel density plot with minor modifications to the code that produces a histogram.

Getting ready

Recall the data from the histogram recipe using the following code:

# Set a seed value to make the data reproducible
set.seed(12345)
cross_tabulation_data <-data.frame(disA=rnorm(n=100,mean=20,sd=3),
                disB=rnorm(n=100,mean=25,sd=4),
                disC=rnorm(n=100,mean=15,sd=1.5),
                age=sample((c(1,2,3,4)),size=100,replace=T),
                sex=sample(c("Male","Female"),size=100,replace=T),
                econ_status=sample(c("Poor","Middle","Rich"),
                size=100,replace=T))

How to do it…

Use the following ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.