# Density Curves

##### Posted on August 16, 2014

The default graphical display of most plotting functions in R is very limited (and usually not very pretty). But that doesn’t mean that we should conform with those crude figures.

### A Basic Plot Example

For this post I’ll use a simple example by plotting density curves of some random generated variables. For instance, let’s generate three normal distributions with different parameters, and their corresponding estimated densities:

We can plot each of the densities with the function plot() which displays a very raw figure. These displays are good for quick visual inspection, but they are very limited for anything else (especially if we plan to include them in some slides for presentation purposes)

### Improved Plots

Of course, we can write some lines of code to substantially improve our graphics. First, let’s define some colors for each distribution using the function hsv()

And now let’s combine them in a single graphic tweaking the right parameters to produce a visually aesthetic plot:

### Using ggplot2

We can also use the package "ggplot2" to obtain a similar graphic. Here’s how:

Published in categories how-to  Tagged with density  plot