Here is a collection of 50+ tips and tricks for ggplot2. Do read them as suggestions, or rule of thumbs, rather than principles and ideas that will generalise across contexts and presentation formats. Most of the tips and tricks are based upon material that is also available via my GitHub repository awesome-ggplot2.
Here we go:
- Familiarise yourself with the different names for theme elements, e.g., by using this reference sheet.
- Do not use shortcuts to create your visualisations (such as quick plots with qplot). It is better to google the correct solutions that you will memorise over time than sticking to less flexible shortcuts.
- Use
geom_braid()
from the package ggbraid to braid two lines and a ribbon.
- When possible, merge two legends into one (see The Data-Ink Lab for an example).
- Use labs() to change all labels on your plot (title, x and y axis, etc.).
- Use ggtext and showtext to use non-default fonts in your plots (see, for example, this guide).
- Use the ggHoriPlot package to visualise time-series data with horizon plots.
- Use country flags in addition to country names on your plot with
geom_flag()
from the ggflags package. - Use
position_dodge()
to make space between bars (see this tweet by Christian Burkhart for an example). - To better illustrate all observations on a scale across groups, consider using
geom_quasirandom()
from the ggbeeswarm package. - Use packages such as geomnet, ggnet, ggnetwork, GGally and qgraph to visualise network data.
- Use the ggradar package to make beautiful radar charts.
- Use packages such as ggcorr and corrmorant to make beautiful correlation plots.
- Use ComplexUpset or ggupset to create UpSet plots.
- Use the gganimate package to create animations.
- Use gghighlight to highlight specific parts of your plot.
- When you have many overlapping lines in a line plot, improve the clarity with
geom_borderline()
from the ggborderline package. - Use
scale_*_log10()
to logarithmize your scales (see this guide for more). - Use packages such as easyalluvial, ggalluvial and ggsankey to make alluvial plots.
- Use the same scaling setting for your figures (see this post for more information).
- Use the ggrepel package to repel overlapping text labels.
- If you are analysing sports data, you can often visualise such data with bespoke packages, e.g. for chess (ggambit) and soccer (ggshakeR and ggsoccer).
- Make Venn diagrams with ggvenn and ggVennDiagram.
- Explore better themes for your plot with packages such as ggthemes, bbplot, and hrbrthemes.
- Use
label_wrap()
from the scales package to deal with long labels (see this post for recommendations on how to deal with long labels). - Be aware that the placing and order of aesthetics matter (see this post for examples).
- If you plot time series data for multiple countries, consider using the function
facet_geo()
from the geofacet package.
- When you use
ggsave()
and want to share your figures (e.g., on social media), use the option bg with “white” to make sure the background is white. - Use the gghalves package to show two geometric objects in the place of one.
- Use the ggchicklet package to make rounded segmented columns (see this tutorial for a great example).
- Use the patchwork package to combine separate ggplots into the same graphic.
- Use
key_glyph
to change the legend keys of geoms (see The Data-Ink Lab for examples). - To make beautiful density estimates based on highest density regions, especially when visualising the correlation between different variables, use the ggdensity package (you can also check out the ggpointdensity package).
- Write wrapper functions instead of copying your code multiple times for almost identical plots (see this tutorial for a great example).
- Use stat_summary() to add uncertainty estimates to your figures (see this tweet by Christian Burkhart for an example).
- Use
<a href="https://yonicd.github.io/ggalt/reference/geom_encircle.html">geom_encircle()</a>
from the ggalt package to automatically enclose points in a polygon.
- Keep yourself posted on new examples of figures made with ggplot2 on social media, e.g., by searching for “ggplot2 min_retweets:10” on Twitter.
- Be familiar with the numerous scales and the different arguments you can use (check out this guide for details on each scale).
- Include images in and on your figures with packages such as ggimg, ggsvg, patternplot, ggpattern, ggimage, and ggbillboard.
- Use the ggdag package to create causal directed acyclic graphs.
- Use
theme(plot.title.position = "plot"))
to left-align your title (see this tutorial for a great example).
- Use
ggthemes::scale_colour_colorblind()
to make sure your figure is colourblind-friendly. For maps, use the colourblind-friendly package viridis. And use the package colorblindcheck to make sure your colours are colourblind-friendly. - Use the
geom_econodist()
function from the ggeconodist package to make beautiful boxplots.
- Combine box plots, violin plots, and jittered points in a raincloud plot (see this tutorial for an example).
- Use icons instead of simple shapes to ease interpretation with packages such as ggwaffle and waffle.
- Explore better colours for your plot with packages such as paletteer, cols4all, ggokabeito, and ggsci.
- You can use specific packages to make detailed maps for areas such as the European Union (eumaps), Spain (mapSpain), Mexico (mxmaps), Denmark (plotDK), and the United States (ggcounty, urbnmapr).
- If you are managing a bigger project, consider creating a Gantt chart with the ganttrify package (see also vistime).
- Use the ggnewscale package to work with multiple colour scales in the same figure (see, e.g., this example).
- Use the package geomtextpath to create curved text paths.
- Follow people working with ggplot2 on Twitter, e.g., Hadley Wickham, Kieran Healy, Claus Wilke and Thomas Lin Pedersen.