The power of ggplot2 is augmented further due to the availability of add-on packages. The remaining changes needed to put the finishing touches on our plot require the ggthemes and ggrepel packages.
The style of a ggplot2 graph can be changed using the theme
functions. Several themes are included as part of the ggplot2
package. In fact, for most of the plots in this book, we use a function
in the dslabs package that automatically sets a default theme:
ds_theme_set()
Many other themes are added by the package ggthemes. Among those are
the theme_economist
theme that we used. After installing the package,
you can change the style by adding a layer like this:
library(ggthemes)
p + theme_economist()
You can see how some of the other themes look by simply changing the
function. For instance, you might try the theme_fivethirtyeight()
theme instead.
The final difference has to do with the position of the labels. In our
plot, some of the labels fall on top of each other. The add-on package
ggrepel includes a geometry that adds labels while ensuring that
they don’t fall on top of each other. We simply change geom_text
with
geom_text_repel
.