This Recipe aims to draw a publish quality density plot from iris
data frame. It usually takes about 2 to 4 lines to craft a very good exploratory bar chart with ggplot2
. Defaults are pretty good but don't fool yourself, there is much more to do in order to achieve publishing quality.
To begin with, generally axes must be grown and texts resized. Many times labels must be rewritten to display the correct name plus it's often good to rework colors. Following section shows how to code this changes with ggplot2
.
Let us start with publish quality density plot:
- Load
ggplot2
and draw a basic density plot:
> library(ggplot2) > hq_1 <- ggplot(data = iris, aes( x = Petal.Length, fill = Species)) + geom_density(alpha = .5, size = 1) + theme_classic()
- Correct axes labels with
xlab()
andylab()
functions. Also correct legends while coercing a new color scale withscale_fill_manual()
:
> hq_2 <- hq_1 + xlab('Petal Lenght...