12 ggplot extensions for snazzier R graphics

For everything from styling text and customizing color palettes to creating your own geoms, these ggplot2 add-ons deserve a place in your R data visualization toolkit. Plus, a bonus list of packages to explore on your own.

1 2 Page 2
Page 2 of 2

ggiraph feels “truer” to the original static graph to me for some complex ggplot graphics. And, ggplot2 subtitles work. Turning a static ggplot into an interactive HTML process requires three steps:

  1. Use a ggiraph interactive geom, such as geom_col_interactive() instead of geom_col();
  2. Add at least one interactive argument to your plot’s aes(). Available arguments include tooltip and data_id; and
  3. Use the girafe() function to make the plot interactive. Since you’re restricted to available ggiraph geoms, custom geoms won’t work here. gghighlight does, though.

For example:

library(ggiraph)
mygraph <- ggplot(snowfall2000s, aes(x = Winter, y = Total, tooltip = Total)) +
  geom_col_interactive(color = "black", fill="#0072B2") +
  theme_minimal() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line =
          element_line(colour = "gray"),
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5)
  ) +
  scale_y_continuous(label = comma) +
  ylab("") + xlab("") +
  labs(title = "Annual Boston Snowfall", subtitle = "2000 to 2016")
girafe(ggobj = mygraph)

There is a lot more you can do with ggiraph, including linking the interactivity of multiple graphs so that mousing over one graph highlights the same data in another. Check out my tutorial video below or this InfoWorld article.

The plotly package is by Carson Sievert and ggiraph is by David Gohel. Both are available on CRAN.

Explain the stats behind your plot: ggstatsplot

ggstatsplot adds written statistical details to visual data analyses, such as looking at correlations, histograms, and violin plots. For example, this code creates a matrix of correlation coefficients looking at relationships in the msleep data set (msleep comes with the ggplot2 package):

library(ggstatsplot)
ggcorrmat(
  data     = ggplot2::msleep,
 colors   = c("#B2182B", "white", "#4D4D4D"),
  title    = "Correlalogram for the msleep mammals sleep data set in ggplot2",
  subtitle = "Sleep units: hours; weight units: kilograms"
)
Correlation matrix showing colored squares, each with number inside Sharon Machlis

Correlation visualization created with ggstats.

Text at the bottom right explains that an X through a box indicates non-significant relationship at a p value less than 0.05.

ggstatsplot is by Indrajeet Patil and is available on CRAN.

Drag-and-drop ggplot: esquisse

If you like a graphical user interface when creating data visualizations, check out esquisse. You can access the package’s GUI by running its RStudio add-in or the esquisser() function from an R console prompt. The resulting interface lets you upload files or use existing objects in your R environment. Then, click to choose graph type, x axis, y axis, fill, color, size, group, and facet, as well as to customize text, number of records, legend position, and more.

Bar graph created with the esquisse R package Sharon Machlis/IDG

Creating an ordered bar graph with the esquisse package.

Once you’ve finalized your graph, you can export it as an image. You also have access to the underlying R code that generated the plot cand can send it back to the R console for re-use or additional tweaking. For example, I didn’t see an option to sort bars by ascending or descending value order, so that’s a task you’d want to do inside R.

See more in the InfoWorld esquisse tutorial or in the video below.

esquisse is by Fanny Meyer, Victor Perrier, and others and is available on CRAN.

Multiple plots: patchwork

If you want to display multiple plots in a single area, patchwork is one of the simplest options. If you’ve saved two plots, one as p1 and the other as p2, p1 + p2 displays them side by side while p1 | p2 places p2 below p1.

patchwork can handle more complex layouts as well. Say you want p1 alone in the first row with p2 and p3 next to each other below: p1 | (p2 + p3). Plots with titles will display those titles in a patchwork grid, but you can also add an overall title and subtitle such as this sightly modified example from the patchwork website:

library(patchwork)
p1 <- ggplot(mtcars) +
  geom_boxplot(aes(gear, disp, group = gear)) +
  ggtitle('Box Plot')
p2 <- ggplot(mtcars) +
  geom_point(aes(mpg, disp)) +
  ggtitle('1st Scatter Plot')
p3 <- ggplot(mtcars) +
  geom_point(aes(hp, wt, colour = mpg)) +
  ggtitle('2nd Scatter Plot')
p1 / (p2 + p3) +
  plot_annotation(title = 'My overall dataviz title',
                  subtitle = 'From the patchwork package',
                  caption = 'Source: mtcars data set'
                  )
Box plot taking up the full page width in the 1st row, 2 scatterplots side by side in the 2nd row Sharon Machlis

Plots arranged in a grid with the patchwork R package.

It’s also possible to add text or tables in the grid, not only plots, although at that point I’d probably be using an R Markdown or Quarto document.

patchwork is by Thomas Lin Pedersen and is available on CRAN.

Another good option is cowplot, which was originally developed as a package to add more themes to ggplot. However, cowplot also includes a plot_grid() function that positions multiple plots in an area and adds labels, for example plot_grid(p1, p2, labels = c('First Plot', 'Second Plot'), label_size = 12).

Gantts with ganttrify

ganttrify is a package with one purpose: Create Gantt charts. It has two functions: ganttrify() to create the charts and shiny_ganttrify() to run an interactive app for chart creation. Check out the package’s included test_project data set to see the format this package expects. 

gantt chart with lines showing various time periods expected for an R project Screen shot by Sharon Machlis

Gantt chart created with the ganttrify R package

ganttrify was created by Giorgio Comai at the European Data Journalism Network. It’s not on CRAN, so install with remotes::install_github("giocomai/ganttrify")

Shiny app allowing you to upload your own data and customize/create a Gantt chart Screen shot by Sharon Machlis

Shiny app to create your own Gantt chart, from the ganttrify R package.

Yet more useful ggplot extensions

There are so many useful ggplot2 extensions that I could go on . . . and on. Here are 10 more that are worth a look.

gganimate is one of the most popular extensions available, “extend[ing] the grammar of graphics as implemented by ggplot2 to include the description of animation,” according to the gganimate website. It provides new classes that describe how a dataviz should change over time.

ggrepel features geoms that ensure text labels don’t overlap each other, appear too close to data points, or bump up to the plot edge.

camcorder tracks and records all ggplots created in an R session so you can eventually create a gif. This package is not on CRAN and must be installed with remotes::install_github("thebioengineer/camcorder", build_vignettes = TRUE).

geomtextpath lets you easily generate curved text labels, such as this example from the package website:

library(geomtextpath)
ggplot(iris, aes(x = Sepal.Length, colour = Species, label = Species)) +
   theme(legend.position = "none") +
   geom_textdensity(size = 6, fontface = 2, spacing = 50, vjust = -0.2, hjust = "ymax") +
   ylim(c(0, 1.3))
Line graph with 3 lines and text labels above the lines that curve Sharon Machlis

Curved labels on a line graph made with the geomtextpath R package.

ggforce has various geoms that offer additional ggplot2 functionality. One I found particularly interesting is facet_zoom(), which creates a second zoomed-in plot from an initial visualization: 

library(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
geom_point() +
facet_zoom(x = Species == "setosa") +
ggsci::scale_color_startrek() # trying ggsci's startrek palette too

Scatter plot with a 2nd scatter plot beneath showing a portion of the 1st plot Sharon Machlis

Plot with a zoomed facet from the ggforce R package (this plot also uses the startrek palette from the ggsci package).

see is part of the easystats family of R packages aimed at the entire workflow of statistical analysis. The see package generates visualizations “for a wide variety of models and statistical analyses in a way that is tightly linked with the model fitting process and requires minimal interruption of users’ workflow,” according to the package site. see uses a single function, plot(), that works with objects generated by other easystats packages. Resulting plots are ggplot2 objects and can be further customized with ggplot2 functions.

The multi-purpose rempsyc package has numerous convenience functions aimed at pyschological research workflow, but it includes a few visualization functions that could be handy for broader use, such as nice_violin() and nice_scatter(). Install with remotes::install_github("rempsyc/rempsyc")

ggbump, as the name implies, offers a geom for creating bump charts — line-and-point charts that compare rankings over time. The default geom_bump() generates curved lines instead of typical line-chart angles between points, and the website offers examples of how to “pimp the bump chart.”

Bump chart showing rankings for Finland, Germany, Sweden, and India over 3 years (toy data) Sharon Machlis

Bump chart with some extra styling created with the ggbump and ggplot2 R packages.

vistime provides easy timeline generation using its gg_vistime() geom. The package can also output Plotly graphs, Highcharts, or data frames, all from data featuring columns for event, start date, end date, and group.

unikn is mostly a package for creating, changing, and viewing color palettes, but it also has two functions that might be useful in a ggplot2 workflow if you’re already using the package for other purposes. usecol() creates a color palette from a vector of colors; newpal() also create a palette, but using both vector of colors and vector of names for those colors. You can then use the palette object as the values argument for ggplot2’s scale_color_manual() or scale_fill_manual(). The package’s seecol() function is especially nice for viewing palettes.

Below is the result of using newpal() to create a version of ColorBrewer’s Dark2 palette with six colors and custom color names, displayed with seecol():

my_dark2_6 <- newpal(c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "#E6AB02"), 
c("green", "orange", "purple", "pink", "lightgreen", "yellow"))
seecol(pal = my_dark2_6, main = "ColorBrewer Dark2 palette with added color names")
Color swatches for green, orange, purple, pink, light green & yellow along with HEX and RGB codes. Sharon Machlis

ColorBrewer Dark2 palette with custom color names displayed with the unikn R package’s seecol() function.

For still more ggplot2 extensions, check out the ggplot2 extensions gallery. And for more R tips, see InfoWorld’s “Do More with R” tutorials page.

Copyright © 2022 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform