Picking a custom number of contour lines
The previous recipe taught you how to create simple yet intuitive contour plots. This one will show how the number of levels/contour lines can be manually picked. There are mainly two arguments to do so and this recipe is demonstrating how these two works. Taking off from the previous recipe framework, let's see how we can go for another amount of lines/polygons.
How to do it...
Let us start with picking a custom number of contour lines:
- Â Directly call
bins
to set the number of levels that the plot will display:
> library(ggplot2) > ggplot(data = cars, aes(x = speed, y = dist)) + geom_density_2d(aes(colour = ..level..), bins = 15)
A great number of bins may be difficult to visualize as the following image (Figure 8.4) shows:
Figure 8.4 - Using bins to set the number of contours
- The
binwidth
argument is an alternative:
> ggplot(data = cars, aes(x = speed, y = dist)) + geom_density_2d(aes(colour = ..level..), binwidth = .0005)
Too few contours...