Visualizing the density of a numeric variable

The density plot is mostly used to compare distributions of two numeric variables or a single numeric variable over the category of other variables. In this recipe, we will produce a density plot using the ggplot2 library.

Getting ready

To display the density, we will recall ggplotdata here and will use the disA variable to display the density plot.

How to do it...

To create a density plot, the basic code is as follows:

ggplot(data=ggplotdata,aes(x=disA))+geom_density()

The following figure shows us the visual output of the preceding code:

How to do it...

How it works…

The basic command is the same with ggplot(), but the main ...

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.