Data visualization with R study guide

Star

General structure

Overview The general structure of the code that is used to plot figures is as follows:

ggplot(...) +             # Initialization
  geom_function(...) +    # Main plot(s)
  facet_function(...) +   # Facets (optional)
  labs(...) +             # Legend (optional)
  scale_function(...) +   # Scales (optional)
  theme_function(...)     # Theme (optional)

We note the following points:


Basic plots The main basic plots are summarized in the table below:

TypeCommand and parametersIllustration
Scatter plotgeom_point(
  x, y, params
)
Line plotgeom_line(
  x, y, params
)
Bar chart

Histogram
geom_bar(
  x, y, params
)
Box plotgeom_boxplot(
  x, y, params
)
Heatmapgeom_tile(
  x, y, params
)

where the possible parameters are summarized in the table below:

CommandDescriptionExample
colorColor of a line / point / border'red'
fillColor of an area'red'
sizeSize of a line / point4
shapeShape of a point4
linetypeShape of a line'dashed'
alphaTransparency, between 0 and 10.3

Remark: this reference provides an extensive list of possible colors.


Maps It is possible to plot maps based on geometrical shapes as follows:

The following table summarizes the main commands used to plot maps:

CategoryActionCommand
MapDraw polygon shapes from the geometry columngeom_sf(data)
Additional elementsAdd and customize geographical directionsannotation_north_arrow(location)
Add and customize distance scaleannotation_scale(location)
RangeCustomize range of coordinatescoord_sf(xlim, ylim)


Animations Plotting animations can be made using the gganimate library. The following command gives the general structure of the code:

# Main plot
ggplot() +
  ... +
  transition_states(field, states_length)

# Generate and save animation
animate(plot, duration, fps, width, height, units, res, renderer)
anim_save(filename)


Advanced features

Facets It is possible to represent the data through multiple dimensions with facets using the following commands:

TypeCommandIllustration
Grid
(1 or 2D)
facet_grid(
  row_var ~ column_var
)
Wrappedfacet_wrap(
  vars(x1, ..., xn),
  nrow, ncol
)


Text annotation Plots can have text annotations with the following commands:

CommandIllustration
geom_text(
  x, y, label,
  hjust, vjust
)
geom_label_repel(
  x, y, label,
  nudge_x, nudge_y
)


Additional elements We can add objects on the plot with the following commands:

TypeCommandIllustration
Linegeom_vline(
  xintercept, linetype
)
geom_hline(
  yintercept, linetype
)
Curvegeom_curve(
  x, y, xend, yend
)
Rectanglegeom_rect(
  xmin, xmax, ymin, ymax
)


Last touch

Legend The title of legends can be customized to the plot with the following command:

plot + labs(params)

where the params are summarized below:

ElementCommand
Title / subtitle of the plottitle = 'text' / subtitle = 'text'
Title of the $x$ / $y$ axisx = 'text' / y = 'text'
Title of the size / colorsize = 'text' / color = 'text'
Caption of the plotcaption = 'text'

This results in the following plot:


Plot appearance The appearance of a given plot can be set by adding the following command:

TypeCommandIllustration
Black and whitetheme_bw()
Classictheme_classic()
Minimaltheme_minimal()
Nonetheme_void()

In addition, theme() is able to adjust positions/fonts of elements of the legend.
Remark: in order to fix the same appearance parameters for all plots, the theme_set() function can be used.


Scales and axes Scales and axes can be changed with the following commands:

CategoryActionCommand
RangeSpecify range of $x$ / $y$ axisxlim(xmin, xmax)
ylim(ymin, ymax)
NatureDisplay ticks in a customized mannerscale_x_continuous()
scale_x_discrete()
scale_x_date()
MagnitudeTransform axesscale_x_log10()
scale_x_reverse()
scale_x_sqrt()

Remark: the scale_x() functions are for the $x$ axis. The same adjustments are available for the $y$ axis with scale_y() functions.


Double axes A plot can have more than one axis with the sec.axis option within a given scale function scale_function(). It is done as follows:

scale_function(sec.axis = sec_axis(~ .))


Saving figure It is possible to save figures with predefined parameters regarding the scale, width and height of the output image with the following command:

ggsave(plot, filename, scale, width, height)