ggplot2 is simply not solely the popular data visualization bundle for the R language, it’s additionally an ecosystem. Fairly a couple 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 greatest to search out out about.
Create your private geoms: ggpackets
As quickly as you’ve bought added a variety of layers and modifications to a ggplot plot, how do you save that work so it’s simple to reuse? A way is to rework your code proper right into a function. One different is to rework 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 a variety of 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 = Entire)) +
geom_col(color = "black", fill="#0072B2") +
theme_minimal() +
theme(panel.border = element_blank(), panel.grid.fundamental = 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)
) +
ylab("") + xlab("")
Right here is how one can convert that to a personalized geom generally known as my_geom_col
:
library(ggpackets)
my_geom_col <- ggpacket() +
geom_col(color = "black", fill="#0072B2") +
theme_minimal() +
theme(panel.border = element_blank(), panel.grid.fundamental = 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)
) +
ylab("") + xlab("")
Remember that I saved each factor in addition to 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 = Entire)) +
my_geom_col()
Graph created with a personalized ggpackets geom.
ggpackets is by Doug Kelkhoff and is accessible from CRAN.
Less complicated ggplot2 code: ggblanket and others
ggplot2 is extraordinarily extremely efficient and customizable, nevertheless sometimes that comes at a worth of complexity. A variety of 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 prefer to suggest you give ggblanket a try. 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 wish 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 decisions all through the options themselves as a substitute 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
items the y-axis title, as a substitute of looking help data for a variety of separate options. ggblanket won’t make it any easier for me bear in mind all these decisions, nevertheless 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 website):
library(ggblanket)
library(palmerpenguins)
penguins |>
gg_histogram(x = body_mass_g, col = species)
Histogram created with ggblanket.
The tip consequence continues to be a ggplot object, which means you presumably can proceed to customize it by together with layers with frequent ggplot2 code.
ggblanket is by David Hodge and is accessible from CRAN.
A variety of totally different packages try to simplify ggplot2 and likewise change its defaults, along with ggcharts. Its simplified options use syntax like
library(ggcharts)
column_chart(snowfall2000s, x = Winter, y = Entire)
That single line of code provides a fairly first price default, plus auto-ordered slashes (you presumably can merely override that).
The bar chart created with ggcharts robotically sorts the bars by values.
Attempt InfoWorld’s ggcharts tutorial or the video beneath for additional particulars.
Straightforward 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, simple to go looking out them using RStudio’s autocomplete.
Should 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 inside the InfoWorld ggeasy tutorial or inside the video beneath.
ggeasy is by Jonathan Carroll et al and is accessible from CRAN.
Highlight elements in your plots: gghighlight
Usually you want to draw consideration to explicit data elements on a chart. You’ll be capable to undoubtedly do this with merely ggplot, nevertheless gghighlight objectives to make it easier. Merely add the gghighlight()
function together with a scenario. As an example, if winters with snow totals bigger than 85 inches are essential to the story I’m telling, I could use gghighlight(Entire > 85)
:
library(gghighlight)
ggplot(snowfall2000s, aes(x = Winter, y = Entire)) +
my_geom_col() +
gghighlight(Entire > 85)
Chart with totals bigger than 85 highlighted with gghighlight.
Or if I want to title explicit years, like 2011-12 and 2014-15, I can set them as my gghighlight()
scenario:
ggplot(snowfall2000s, aes(x = Winter, y = Entire)) +
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, nevertheless it’s possible you’ll want to flick through them to go looking out ones which have themes or palettes that enchantment to you.
After placing in one among these packages, you presumably can sometimes use a model new theme or color palette within the similar signifies 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()
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 footage and TV displays” 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, nevertheless it has a great deal of themes to pick out from, plus a GitHub repository that makes it simple to go looking 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 significantly 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 normally a bit overwhelming at first, nevertheless it’s possible you’ll almost undoubtedly uncover a palette that appeals to you.
Remember that you need to make the most of none R color palette with ggplot, even if 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 varieties to ggplot2 textual content material: ggtext
The ggtext bundle makes use of Markdown-like syntax in order so as to add varieties and colors to textual content material inside a chart. As an 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 = Entire)) +
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 website:
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")
Rain cloud plot generated with the ggdist bundle.
Go to the ggdist website for full particulars and further 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 resolution 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 very simple 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. As an example:
library(plotly)
ggplotly(
ggplot(snowfall2000s, aes(x = Winter, y = Entire)) +
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.
Remember 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 = ~Entire, form = "bar")