Tips and tricks for ggplot2

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:

1. Familiarise yourself with the different names for theme elements, e.g., by using this reference sheet.
2. 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.
3. Use geom_braid() from the package ggbraid to braid two lines and a ribbon.
4. When possible, merge two legends into one (see The Data-Ink Lab for an example).
5. Use labs() to change all labels on your plot (title, x and y axis, etc.).
6. Use ggtext and showtext to use non-default fonts in your plots (see, for example, this guide).
7. Use the ggHoriPlot package to visualise time-series data with horizon plots.
8. Use country flags in addition to country names on your plot with geom_flag() from the ggflags package.
9. Use position_dodge() to make space between bars (see this tweet by Christian Burkhart for an example).
10. To better illustrate all observations on a scale across groups, consider using geom_quasirandom() from the ggbeeswarm package.
11. Use packages such as geomnet, ggnet, ggnetwork, GGally and qgraph to visualise network data.
13. Use packages such as ggcorr and corrmorant to make beautiful correlation plots.
14. Use ComplexUpset or ggupset to create UpSet plots.
15. Use the gganimate package to create animations.
16. Use gghighlight to highlight specific parts of your plot.
17. When you have many overlapping lines in a line plot, improve the clarity with geom_borderline() from the ggborderline package.
18. Use scale_*_log10() to logarithmize your scales (see this guide for more).
19. Use packages such as easyalluvial, ggalluvial and ggsankey to make alluvial plots.
21. Use the ggrepel package to repel overlapping text labels.
22. 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).
23. Make Venn diagrams with ggvenn and ggVennDiagram.
24. Explore better themes for your plot with packages such as ggthemes, bbplot, and hrbrthemes.
25. Use label_wrap() from the scales package to deal with long labels (see this post for recommendations on how to deal with long labels).
26. Be aware that the placing and order of aesthetics matter (see this post for examples).
27. If you plot time series data for multiple countries, consider using the function facet_geo() from the geofacet package.
28. 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.
29. Use the gghalves package to show two geometric objects in the place of one.
30. Use the ggchicklet package to make rounded segmented columns (see this tutorial for a great example).
31. Use the patchwork package to combine separate ggplots into the same graphic.
32. Use key_glyph to change the legend keys of geoms (see The Data-Ink Lab for examples).
33. 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).
34. Write wrapper functions instead of copying your code multiple times for almost identical plots (see this tutorial for a great example).
35. Use stat_summary() to add uncertainty estimates to your figures (see this tweet by Christian Burkhart for an example).
36. Use geom_encircle() from the ggalt package to automatically enclose points in a polygon.
37. 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.
38. Be familiar with the numerous scales and the different arguments you can use (check out this guide for details on each scale).
39. Include images in and on your figures with packages such as ggimg, ggsvg, patternplot, ggpattern, ggimage, and ggbillboard.
40. Use the ggdag package to create causal directed acyclic graphs.
41. Use theme(plot.title.position = "plot")) to left-align your title (see this tutorial for a great example).
42. 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.
43. Use the geom_econodist() function from the ggeconodist package to make beautiful boxplots.
44. Combine box plots, violin plots, and jittered points in a raincloud plot (see this tutorial for an example).
45. Use icons instead of simple shapes to ease interpretation with packages such as ggwaffle and waffle.
46. Explore better colours for your plot with packages such as paletteer, cols4all, ggokabeito, and ggsci.
47. 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).
48. If you are managing a bigger project, consider creating a Gantt chart with the ganttrify package (see also vistime).
49. Use the ggnewscale package to work with multiple colour scales in the same figure (see, e.g., this example).
50. Use the package geomtextpath to create curved text paths.
51. Follow people working with ggplot2 on Twitter, e.g., Hadley Wickham, Kieran Healy, Claus Wilke and Thomas Lin Pedersen.