A density plot, like a histogram of frequency, helps visualize the distribution of a sample. This time, instead of bars, a single curve is drawn to represent that distribution.

Let’s use ggplot() to draw the density plot for a data set generated by rnorm() (read more here about rnorm()). Here is the dataframe:

# dataframe
df <- data.frame(ID, values)
str(df)
## 'data.frame':    200 obs. of  2 variables:
##  $ ID    : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ values: num  56.8 58.3 102.2 59 71.2 ...



We first map the data from the variable values by typing ggplot(df, aes(values)) and then use geom_density() to draw the plot:

ggplot(df, aes(values)) + 
  geom_density()

To realize how similar a density plot and a histogram are, we can put them next to each other:

ggplot(df, aes(values)) +            # histogram
  geom_histogram(bins = 30)
ggplot(df, aes(values)) +            # density plot
  geom_density()

There isn’t much you can do to improve the look of a density plot, but you can alway add some colors with fill= and color=, or make the line thicker with size=:

ggplot(df, aes(values)) +  
  geom_density(fill="red", color="blue", size=1) 



If this is a bit to bright for your eyes, you may add some transparency with alpha=:

ggplot(df, aes(values)) +  
  geom_density(fill="red", alpha=.2) 



Adding plot title, axis titles, ticks, labels and other essential elements

In this section, you will learn how to set/modify all the necessary elements that make a plot complete and comprehensible. Such elements are: