Figures
Last updated: January 18, 2021
Table of contents:
Getting decent-looking plots with ggplot2
There’s a list of all the ggplot2
themes, but I don’t think any of them are great – they all have at least one weird element (no axis lines, hidden axis labels, non-white backgrounds, etc.).
Instead of the defaults, I like using ggpubr
, which provides a nicer set of themes suitable for publications and presentations.
The BBC has a set of defaults for ggplot2
that is worth looking at.
The default color schemes aren’t great either. ggsci
provides some good alternatives.
The R Graph Gallery is a good resource for good-looking plots.
ggplot GUI is an online interface for dynamically exploring ggplot2
plots.
ASCII plots
Sometimes it’s helpful to have ASCII (text-based) plots. txtplot
is a package for doing this:
> library(txtplot)
> with(cars, txtplot(speed,dist))
+---+----------+----------+---------+----------+-+
120 + * +
100 + +
| * |
80 + * * * +
| * * * |
60 + * * * * +
| * * * * * * |
40 + * * * * * +
| * * * * * * * * * |
20 + * * * * * * +
| * * * |
0 +---+----------+----------+---------+----------+-+
5 10 15 20 25
Figures
Arranging multiple figures
There are a few ways to do this:
Themes
See Complete themes – ggplot2.
Tables for publication
tangram
is a tool for creating “rich summary tables”.table1
is another interesting package for publication-quality tables.flextable
can produce well-formatted HTML tables from data frames.
kintr::kable
I’ve had great luck with making custom tables for publication with knitr::kable
and kableExtra
.
Some resources:
- Create Awesome HTML Table with knitr::kable and kableExtra
- If you need to use variables inside add_header_above()
, you can use setNames(2, varname)
.
I was not able to figure out an easy way to reference tables and figures in R Markdown text. The captioner
package is supposed to fix this, but it hasn’t been updated in 3 years so I’m not sure how well it actually works.
âšī¸ This page is part of my knowledge base for R, the popular statistical programming language. I attempt to use idiomatic practices with the tidyverse
collection of packages as much as possible. If you have suggestions for ways to improve this code, please contact me or use the survey link below..