2.5 Noteworthy ggplot details
ggplotrequires data and maps independent variables to dimensions (e.g., the x- and y-axis) and dependent variables to geometric objects (called geoms). It typically assumes that the to-be-plotted
<DATA>is in a table (data frame or tibble) in long format and contains independent variables as factors.
mappings =can be omitted, but an aesthetic mapping
aes(<MAPPING>)for at least one geom is needed.
Different geoms can be combined into layers, but their order matters (as later layers are printed on top of earlier ones).
When multiple geoms use the same aesthetic mappings, their common
aes(<MAPPING>)can be moved into the initial
ggplotcall, a sequence of commands is combined by
+, rather than
%>%, which is the forward pipe operator provided by the magrittr package (Bache & Wickham, 2014). The
+has to be at the end of the current line, rather than at the beginning of the next line. (See Section 3.3 for more information about the forward pipe operator.)
Both the content and the visual appearance of plots are highly customizable (e.g., by supplying many aesthetic arguments, combining multiple geoms in one plot, speciying labels and legends, and applying pre-defined themes to plots). Tuning plots can be a lot of fun (see Section 4.2.11 for some examples), but always keep in mind your current goals and the plot’s intended audience.
Bache, S. M., & Wickham, H. (2014). magrittr: A forward-pipe operator for R. Retrieved from https://CRAN.R-project.org/package=magrittr
Wickham, H. (2017). tidyverse: Easily install and load the ’tidyverse’. Retrieved from https://CRAN.R-project.org/package=tidyverse
Wickham, H., Chang, W., Henry, L., Pedersen, T. L., Takahashi, K., Wilke, C., … Yutani, H. (2019). ggplot2: Create elegant data visualisations using the grammar of graphics. Retrieved from https://CRAN.R-project.org/package=ggplot2