This document is an introduction to plotting using ggplot. ggplot is powerfull tool for plotting basically anything you could think of and it allows you to modify all aspects of your plot, which makes ggplot much more flexible than the plot function in base R. I hope that this document can help you to get an understanding of how ggplots are build. This should help you understand why problems occur when plotting and help you solve them.
This document includes two parts; The first part is a short introduction to the layers of a ggplot and their functionalities. The second part contains suggestions of additional packages, that can help you improve your graphic work. Examples are provided with code and graphs throughout the document.
A ggplot consists of different layers that can be combined to a graph. The differnt layers will add an extra layer of information to the plot. You can (in most cases) add several layers of the same type, e.g. two different geometries can be added and will create one plot with two types of graphs inside. In the following the different layers and their content is discribed breifely and examples will follow afterwards.
Data is most often defined in ggplot(), but the mapping of data using aes() can be written under both ggplot() and the geometries. If all geometries are using the same mapping of data, then it does not matter if aes() is defined in ggplot() or under the geometry. If they use differnt mappings, aes() will have to be defined under each geometry.
Here a plot with two geometries are shown with the two different codes - they result in the same plot. If the different geometries were relying on differnt data sources, then data must be defined under each geometry, you can see an example here: Maps
#A
cloud1<-ggplot(dat,aes(x = age, y = bmi))+
geom_point(alpha=0.3)+
geom_density_2d()
#B
cloud2<-ggplot(dat)+
geom_point(aes(x = age, y = bmi), alpha=0.3)+
geom_density_2d(aes(x = age, y = bmi))