6.6 Customization etc.
There are many ways in which graphs can (and often: ought to) be tweaked further. The following can only cover a small, but hopefully useful selection.
6.6.1 Themes
The general appearance of a plot is governed by its theme. There are many ready-made themes already in the ggplot
package, as listed here, and there are more in several other packages. If we store a plot in a variable we can look at how different themes affect it.
<- avocado_data_early_late %>%
avocado_grid_plot ggplot(aes(x = log(total_volume_sold), y = average_price)) +
geom_point(alpha = 0.3, color = "skyblue") +
geom_smooth(method = "lm", color = "darkorange") +
facet_grid(type ~ early)
+ theme_classic() avocado_grid_plot
+ theme_void() avocado_grid_plot
+ theme_dark() avocado_grid_plot
The plots in this book use the theme hrbrthemes::theme_ipsum
from the hrbrthemes
package as a default. You can set the default theme for all subsequent plots using a command like this:
# set the 'void' theme as global default
theme_set(
theme_void()
)
More elaborate tweaking of a plot’s layout can be achieved by the theme
function. There are many options. Some let you do crazy things:
+ theme(plot.background = element_rect(fill = "darkgreen")) avocado_grid_plot
6.6.2 Guides
When using grouped variables (by color, shape, linetype, group, …) ggplot
creates a legend automatically.
%>%
avocado_data ggplot(
mapping = aes(
x = log(total_volume_sold),
y = average_price,
color = type
)+
) geom_point(alpha = 0.5)
The legend can be suppressed with the guides
command. It takes as arguments the different types of grouping variables (like color
, group
, etc.).
%>%
avocado_data ggplot(
mapping = aes(
x = log(total_volume_sold),
y = average_price,
color = type
)+
) geom_point(alpha = 0.5) +
# no legend for grouping by color
guides(color = "none")
6.6.3 Axes, ticks and tick labels
If you need to use a non-standard (Cartesian) axis, you can do so, e.g., by changing the \(x\)-axis to a log scale (with base 10):
%>%
avocado_data ggplot(
mapping = aes(
x = total_volume_sold,
y = average_price,
color = type
)+
) geom_point(alpha = 0.5) +
scale_x_log10()
The scales
package has a number of nice convenience functions for tweaking axis ticks (the places where axes are marked and possibly labeled) and tick labels (the labels applied to the tick marks). For example, we can add dollar signs to the price information, like so:
%>%
avocado_data ggplot(
mapping = aes(
x = total_volume_sold,
y = average_price,
color = type
)+
) geom_point(alpha = 0.5) +
scale_x_log10() +
scale_y_continuous(labels = scales::dollar)
6.6.4 Labels
To change any other kind of labeling information (aside from tick mark labels on axes), the labs
function can be used. It is rather self-explanatory:
%>%
avocado_data ggplot(
mapping = aes(
x = total_volume_sold,
y = average_price,
color = type
)+
) geom_point(alpha = 0.5) +
scale_x_log10() +
scale_y_continuous(labels = scales::dollar) +
# change axis labels and plot title & subtitle
labs(
x = 'Total volume sold (on a log scale)',
y = 'Average price',
title = "Avocado prices plotted against the amount sold per type",
subtitle = "With linear regression lines",
caption = "This plot shows the total volume of avocados sold against the average price for many different points in time."
)
6.6.5 Combining & arranging plots
Presenting visual information in a tightly packed spatial arrangement can be helpful for the spectator. Everything is within a single easy saccade, so to speak. Therefore it can be useful to combine different plots into a single combined plot. The cowplot
package helps with this, in particular the function cowplot::plot_grid
as shown here:
# create an avocado plot
<- avocado_data %>%
avocado_plot ggplot(aes(x = total_volume_sold, y = average_price)) +
geom_point(alpha = 0.5)
# create a BLJM bar plot
<- data_BLJM_processed %>%
BLJM_plot ggplot(aes(x = response)) +
geom_bar()
# combine both into one
::plot_grid(
cowplot# plots to combine
avocado_plot,
BLJM_plot,# number columns
ncol = 1
)
6.6.6 LaTeX expressions in plot labels
If you are enthusiastic about LaTeX, you can also use it inside of plot labels. The latex2exp
package is useful here, which provides the function latex2exp::TeX
to allow you to include LaTeX formulas. Just make sure that you double all backslashes, as in the following example:
%>%
avocado_data ggplot(aes(x = total_volume_sold, y = average_price)) +
geom_point(alpha = 0.5) +
labs(title = latex2exp::TeX("We can use $\\LaTeX$ here: $\\sum_{i = 0}^n \\alpha^i$"))
Exercise 6.4: Customization
Feel free to play around with customizing your previously created plots or plots that you find in this book. Try to make annotations or try out different themes and colors. It will help you understand these kinds of plots a little better.