12 ggplot extensions for snazzier R graphics

ggplot2 is simply not solely the popular data visualization bundle for the R language, it’s additionally an ecosystem. Fairly a number of add-on packages give ggplot additional vitality to do each factor from additional merely altering axis labels to robotically producing statistical information and customizing . . . almost each factor.

Listed below are a dozen good ggplot2 extensions it’s finest to search out out about.

Create your private geoms: ggpackets

As quickly as you’ve acquired added quite a few layers and adjustments to a ggplot plot, how do you save that work so it’s simple to reuse? A method is to remodel your code proper right into a function. One different is to remodel it to an RStudio code snippet. Nonetheless the ggpackets bundle has a additional ggplot-friendly means: Create your private personalized geom! It is as simple as storing it in a variable using the ggpacket() function.

The following sample code creates a bar chart from the Boston snowfall data and has quite a few strains of customizations that I want to reuse with totally different data. The first block of code is the preliminary graph:

library(ggplot2)
library(scales)
library(rio)
snowfall2000s <- import("https://gist.githubusercontent.com/smach/5544e1818a76a2cf95826b78a80fc7d5/raw/8fd7cfd8fa7b23cba5c13520f5f06580f4d9241c/boston_snowfall.2000s.csv")
ggplot(snowfall2000s, aes(x = Winter, y = Complete)) +
  geom_col(color = "black", fill="#0072B2") +
  theme_minimal() +
  theme(panel.border = element_blank(), panel.grid.essential = element_blank(),
        panel.grid.minor = element_blank(), axis.line =
          element_line(shade = "gray"),
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5)
  ) +
  ylab("") + xlab("")

Right here is how one can convert that to a personalized geom often called my_geom_col:

library(ggpackets)
my_geom_col <- ggpacket() +
  geom_col(color = "black", fill="#0072B2") +
  theme_minimal() +
  theme(panel.border = element_blank(), panel.grid.essential = element_blank(),
        panel.grid.minor = element_blank(), axis.line =
          element_line(shade = "gray"),
        plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5)
  ) +
  ylab("") + xlab("")

Bear in mind that I saved each factor moreover the distinctive graphic first ggplot() line of code to the personalized geom.

That’s how simple it’s to utilize that new geom:

ggplot(snowfall2000s, aes(x = Winter, y = Complete)) +
  my_geom_col()
bar chart with blue bars Sharon Maclis

Graph created with a personalized ggpackets geom.

ggpackets is by Doug Kelkhoff and is accessible from CRAN.

Easier ggplot2 code: ggblanket and others

ggplot2 is extraordinarily extremely efficient and customizable, nonetheless usually that comes at a price of complexity. Various packages intention to optimize ggplot2 to make frequent data visualizations easier or additional intuitive.

In case you might be inclined to neglect which geoms to utilize for what, I wish to suggest you give ggblanket a attempt. One amongst my favorite points regarding the bundle is that it merges column and fill aesthetics proper right into a single column aesthetic, so I not need to recollect whether or not or to not make use of 1. scale_fill_ each scale_colour_ function.

One different advantage of ggblanket: its geomes as gg_col() each gg_point() embrace customization selections all through the options themselves in its place of requiring separate layers. And which means I solely need to take a look at a help file to see points like pal is to stipulate a color palette and y_title models the y-axis title, in its place of searching help data for quite a few separate options. ggblanket won’t make it any easier for me take into accout all these selections, nonetheless they’re easier to uncover.

Right here is how one can generate a histogram from the Palmer’s penguin dataset with ggblanket (occasion taken from the bundle site):

library(ggblanket)
library(palmerpenguins)
penguins |>
  gg_histogram(x = body_mass_g, col = species)
Histogram with 3 colors and a legend Sharon Maclis

Histogram created with ggblanket.

The tip end result continues to be a ggplot object, which means you presumably can proceed to customize it by together with layers with widespread ggplot2 code.

ggblanket is by David Hodge and is accessible from CRAN.

Various totally different packages try and simplify ggplot2 and likewise change its defaults, along with ggcharts. Its simplified options use syntax like

library(ggcharts)
column_chart(snowfall2000s, x = Winter, y = Complete)

That single line of code provides a fairly first fee default, plus auto-ordered slashes (you presumably can merely override that).

Bar chart with blue bars sorted by ascending values Sharon Maclis

The bar chart created with ggcharts robotically sorts the bars by values.

Attempt InfoWorld’s ggcharts tutorial or the video below for additional particulars.

Simple textual content material customization: ggeasy

ggeasy doesn’t affect the “core” part of your data present, i.e. bar/dot/line sizes, colors, orders, and plenty of others. As an alternative, it’s all about customizing the textual content material throughout the charts, corresponding to labels and axis formatting. All ggeasy options start with easy_ so it’s, positive, easy to look out them using RStudio’s autocomplete.

Must center a plot title? easy_center_title(). Do you want to rotate the x-axis labels by 90 ranges? easy_rotate_labels(which = "x").

Be taught additional regarding the bundle throughout the InfoWorld ggeasy tutorial or throughout the video below.

ggeasy is by Jonathan Carroll et al and is accessible from CRAN.

Highlight components in your plots: gghighlight

Sometimes you want to draw consideration to explicit data components on a chart. You’ll have the ability to undoubtedly do that with merely ggplot, nonetheless gghighlight targets to make it easier. Merely add the gghighlight() function together with a state of affairs. For example, if winters with snow totals bigger than 85 inches are essential to the story I’m telling, I’ll use gghighlight(Complete > 85):

library(gghighlight)
ggplot(snowfall2000s, aes(x = Winter, y = Complete)) +
  my_geom_col() +
  gghighlight(Complete > 85)
Bar chart with 2 blue bars highlighted and the rest grey. Sharon Maclis

Chart with totals bigger than 85 highlighted with gghighlight.

Or if I want to identify explicit years, like 2011-12 and 2014-15, I can set them as my gghighlight() state of affairs:

ggplot(snowfall2000s, aes(x = Winter, y = Complete)) +
  my_geom_col() +
  gghighlight(Winter %in% c('2011-12', '2014-15'))

gghighlight is by Hiroaki Yutani and is accessible from CRAN.

Add themes or color palettes: ggthemes and others

The ggplot2 ecosystem consists of a wide range of packages in order so as to add themes and color palettes. You perhaps is not going to need all of them, nonetheless chances are you’ll want to flick via them to look out ones which have themes or palettes that enchantment to you.

After placing in one amongst these packages, you presumably can usually use a model new theme or color palette within the an identical implies that you’d use a built-in ggplot2 theme or palette. Right here is an occasion with the solarized theme and colorblind palette from the ggthemes bundle:

library(ggthemes)
ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g, color = species)) +
  geom_point() +
  ggthemes::theme_solarized() +
  scale_color_colorblind()
Scatter chart with pale yellow background Sharon Maclis

Scatterplot using a colorblind palette and a solarized theme from the ggthemes bundle.

ggthemes is by Jeffrey B. Arnold et al and is accessible from CRAN.

Totally different theme packs and palettes to ponder:

ggsci is a set of ggplot2 color palettes “impressed by scientific journals, data visualization libraries, science fiction movement photos and TV reveals” corresponding to scale_fill_lancet() Y scale_color_startrek().

hrbrthemes is a popular theme pack that focuses on typography.

ggthemr is a bit a lot much less well-known than the others, nonetheless it has a great deal of themes to pick out from, plus a GitHub repository that makes it easy to look out themes and see what they appear like.

bbplot has only one theme, bbc_style()the BBC’s ready-to-publish mannequin, along with a second attribute to avoid wasting a number of a plot for publication, finalise_plot().

paletteer is a metapackage that mixes palettes from dozens of separate R palette packages into one with a single fixed interface. And that interface consists of options notably for ggplot to utilize, with syntax like scale_color_paletteer_d("nord::aurora"). Proper right here nord it’s the genuine palette pack Title, aurora is the exact palette establish, and the _d implies that this palette is for discrete (not regular) values. palette is often a bit overwhelming at first, nonetheless chances are you’ll almost undoubtedly uncover a palette that appeals to you.

Bear in mind that it is best to make the most of none R color palette with ggplot, even whenever you do not need ggplot-specific color scaling options, with ggplot’s handbook scaling options and color palette values, corresponding to scale_color_manual(values=c("#486030", "#c03018", "#f0a800")).

Add color and totally different sorts to ggplot2 textual content material: ggtext

The ggtext bundle makes use of Markdown-like syntax in order so as to add sorts and colors to textual content material inside a chart. For example, underscores spherical textual content material add italics, and two asterisks spherical textual content material create a daring mannequin. For this to work appropriately with ggtext, the bundle element_markdown() The function ought to even be added to a ggplot theme. The syntax is in order so as to add the appropriate markdown mannequin to the textual content material Y Then add element_markdown() to theme issuelike this to italicize a subtitle:

library(ggtext)
ggplot(snowfall2000s, aes(x = Winter, y = Complete)) +
  my_geom_col() +
  labs(title = "Annual Boston Snowfall", subtitle = "_2000 to 2016_") +
  theme(
    plot.subtitle = element_markdown()
  )

ggtext is by Claus O. Wilke and is accessible from CRAN.

Convey uncertainty: ggdist

ggdist gives geoms to visualise data distribution and uncertainty, producing graphs like rain cloud plots and logit plots with new geoms like stat_slab() Y stat_dotsinterval(). Proper right here is an occasion from the ggdist site:

library(ggdist)
set.seed(12345) # for reproducibility
data.physique(
  abc = c("a", "b", "b", "c"),
  price = rnorm(200, c(1, 8, 8, 3), c(1, 1.5, 1.5, 1))
) %>%
  ggplot(aes(y = abc, x = price, fill = abc)) +
  stat_slab(aes(thickness = stat(pdf*n)), scale = 0.7) +
  stat_dotsinterval(side = "bottom", scale = 0.7, slab_size = NA) +
  scale_fill_brewer(palette = "Set2")
Three rain cloud graphics, each a different color Sharon Maclis

Rain cloud plot generated with the ggdist bundle.

Go to the ggdist site for full particulars and additional examples. ggidst is by Matthew Kay and is accessible from CRAN.

Add interactivity to ggplot2: plotly and ggiraph

In case your charts are going to the online, it’s your choice them to be interactive, offering choices like turning assortment on and off and displaying underlying data if you happen to hover over some extent, line, or bar. Every plotly and ggiraph flip ggplots into interactive HTML widgets.

plotly, an R wrapper for the plotly.js JavaScript library, is awfully easy to utilize. All you do is put your final ggplot contained within the bundle ggplotly() function, and the function returns an interactive mannequin of your diagram. For example:

library(plotly)
ggplotly(
ggplot(snowfall2000s, aes(x = Winter, y = Complete)) +
  geom_col() +
  labs(title = "Annual Boston Snowfall", subtitle = "2000 to 2016")
)

plotly works with totally different extensions, along with ggpackets and gghighlights. plotly’s plots don’t always embrace each factor that appears in a static mannequin (as of this writing, it didn’t acknowledge ggplot2’s subtitles, as an example). Nonetheless the bundle is hard to beat for fast interactivity.

Bear in mind that the plotly library moreover has a function unrelated to ggplot, plot_ly()which makes use of syntax very similar to that of ggplot qplot():

plot_ly(snowfall2000s, x = ~Winter, y = ~Complete, type = "bar")

By admin

x