12 ggplot extensions for snazzier R graphics

Uncategorized

< img src =" https://images.idgesg.net/images/article/2018/05/cw_r_guide_video_series_thinkstock_16x9_1920x1080-100758020-large.jpg?auto=webp&quality=85,70 "alt ="" > ggplot2 is not just the R language’s most popular information visualization package, it is likewise an ecosystem. Various add-on packages provide ggplot added power to do whatever from more quickly changing axis labels to auto-generating statistical information to customizing … almost anything.Here are a lots

great ggplot2 extensions you should know.Create your own geoms: ggpackets As soon as you’ve added multiple layers

and tweaks to a ggplot graph, how can you save that work so it’s easy to re-use? One way is to convert your code into a function. Another is to turn it into an RStudio code bit. But the ggpackets bundle has a ggplot-friendlier way: Produce your own customized geom! It’s as pain-free as keeping it in a variable using the ggpacket () function.The example code below creates a bar chart from Boston snowfall data, and it has a number of lines of personalizations that I ‘d like to use again with other information. The first code block is the preliminary graph: library (ggplot2 )library (scales) library( rio) snowfall2000s gg_histogram( x

= body_mass_g, col= species)< img alt =" Pie chart with 3 colors and a legend" width =" 1200 "height=" 704 "src="

https://images.idgesg.net/images/article/2022/07/12ggplot_02-100930419-large.jpg?auto=webp&quality=85,70″/ > Sharon Machlis Histogram developed with ggblanket. The outcome is still a ggplot object, which suggests you can continue personalizing it by including layers with traditional ggplot2 code.ggblanket is by David Hodge and is offered on CRAN.Several other plans try to streamline ggplot2 and change its defaults, too, consisting of ggcharts. Its streamlined functions utilize syntax like library (ggcharts) column_chart( snowfall2000s, x= Winter season, y= Overall )That single line of code offers a pretty good default, plus instantly sorted bars( you can quickly bypass that ).< img alt =" Bar chart with blue bars arranged by ascending worths" width =" 1200" height =” 704″ src=” https://images.idgesg.net/images/article/2022/07/12ggplot_03-100930420-large.jpg?auto=webp&quality=85,70 “/ > Sharon Machlis Bar chart produced with ggcharts immediately sorts the bars by values. See the InfoWorld ggcharts tutorial or the video below for more details.=” 560″ height= “315 “frameborder=” 0″ > Easy text customization: ggeasy does not impact the” main” part of your dataviz– that is, the bar/point/line sizes, colors, orders, and so on. Instead, it’s everything about personalizing the text around the plots, such as labels
and axis formatting. All ggeasy functions start with easy _ so it’s, yes, simpleHistogram with 3 colors and a legend to find them utilizing RStudio auto-complete.

Need to focus a plot title? easy_center_title (). Want to rotate x-axis labels 90 degrees? easy_rotate_labels( which=” x”

). Learn more about the package in the InfoWorld ggeasy tutorial or the video listed below. ggeasy is by Jonathan Carroll and others and is readily available on CRAN.Highlight products in your plots: gghighlight Sometimes you wish to call attention to particular information points in a chart. You can definitely do that with ggplot alone, however gghighlight aims to make it much easier. Just add the gghighlight() function together with

Bar graph with blue bars sorted by ascending values a condition. For example, if winter seasons with overall snowfall higher than 85 inches are very important to the story I’m telling, I might utilize gghighlight( Total > 85): library( gghighlight) ggplot( snowfall2000s, aes( x= Winter season, y= Total ))+ my_geom_col( )+ gghighlight( Overall > 85)< img

alt=” Bar graph with 2 blue bars highlighted

and the rest grey. “width =” 1200″ height=” 704″ src=” https://images.idgesg.net/images/article/2022/07/12ggplot_04-100930421-large.jpg?auto=webp&quality=85,70 “/ > Sharon Machlis Chart with totals over 85 highlighted using gghighliight. Or if I want to call out particular years, such as 2011-12 and 2014-15, I can set those as my gghighlight() condition: ggplot( snowfall2000s, aes( x= Winter season, y= Total ))+ my_geom_col () +gghighlight( Winter season % in% c(

‘ 2011-12’, ‘2014-15’ )) gghighlight is by Hiroaki Yutani and is available on CRAN.Add styles or color schemes

: ggthemes and others The ggplot2 environment consists of a number of bundles

to add styles and color schemes. You likely won’t

need them all, but you might wish to check out them to discover ones that have styles or combinations you discover compelling.After setting up among these plans, you can normally use a brand-new style or color palette in the very same method that you ‘d use a built-in ggplot2 theme or combination. Here’s an example with the ggthemes bundle’s solarized style and colorblind palette: library (ggthemes) ggplot( penguins, aes (x =bill_length_mm, y =body_mass_g, color= species))

+ geom_point()
+ ggthemes:: theme_solarized() +scale_color_colorblind( )< img alt=" Scatter
plot with pale yellow background” width=” 1200″ height=” 704″ src=” https://images.idgesg.net/images/article/2022/07/12ggplot_05-100930422-large.jpg?auto=webp&quality=85,70″/ > Sharon Machlis Scatter plot using a colorblind scheme and solarized theme from the ggthemes bundle. ggthemes is by Jeffrey B. Arnold and others and is offered on CRAN.Other style and combination packages to think about:
ggsci is

a collection of ggplot2 color schemes” inspired by scientific journals, data visualization libraries, science fiction films, and television programs” such as scale_fill_lancet () and scale_color_startrek(). hrbrthemes is a popular style plan with a concentrate on typography.ggthemr is a bit less well known than those others, however it has a lot of styles to choose from plus a GitHub repo that makes it easy to browse styles and see what they look like.bbplot has just a single theme, bbc_style (), the publication-ready style of the BBC, as well as a 2nd function to save a plot for publication, finalise_plot(). paletteer is a meta package, integrating combinations from dozens of separate R palette bundles into one with a single consistent user interface. Which interface includes functions particularly for ggplot utilize, with a syntax such as scale_color_paletteer_d(” nord:: aurora “). Here nord is the original scheme bundle name, aurora is the particular combination name, and the _ d signifies that this palette is for discreet worths (
not constant).
paletteer can be a little frustrating initially, however you will practicallyScatter plot with pale yellow background certainly find a combination that attract you.Note that you can use any R color palette with ggplot, even if it does not have ggplot-specific color scale functions, with ggplot’s manual scale functions and the color palette worths, such as scale_color_manual( worths= c(” # 486030″,” #c 03018″,” #f 0a800″)). Include color and other styling to ggplot2 text: ggtext The ggtext package utilizes markdown-like syntax to include designs and colors to text within a plot. For instance, underscores surrounding the text include italics and 2 asterisks around the text create strong styling. For this to work appropriately with ggtext, the bundle’s element_markdown() function need to beadded to a ggplot style, too. Syntax is to add the suitable markdown styling to the text and then add element_markdown() to the aspect of the theme, such as this for italicizing a subtitle: library( ggtext) ggplot( snowfall2000s, aes (x= Winter season, y= Total ))+ my_geom_col()+ labs (title =” Yearly Boston Snowfall”, subtitle=” _ 2000 to 2016 _ “)+ theme (plot.subtitle= element_markdown()

) ggtext is by Claus O. Wilke and is available on CRAN.Convey uncertainty: ggdist adds geoms for envisioning data circulation and uncertainty, creating graphics like storm cloud plots and logit dotplots with new geoms like stat_slab () and stat_dotsinterval (). Here’s one example from the ggdist website: library( ggdist )set.seed( 12345) # for reproducibility data.frame( abc= c(” a”, “b”,” b “, “c”), worth =rnorm( 200, c( 1, 8, 8, 3), c (1, 1.5, 1.5, 1)) )% >% ggplot (aes (y = abc, x= worth, fill= abc) )+ stat_slab (aes (density= stat( pdf * n)), scale= 0.7) +stat_dotsinterval(

side =” bottom “, scale = 0.7, slab_size= NA )+ scale_fill_brewer( scheme =” Set2″)< img alt=" 3 raincloud plots, each a different color" width =" 1200" height =" 704" src=" https://images.idgesg.net/images/article/2022/07/12ggplot_06-100930423-large.jpg?auto=webp&quality=85,70"/ > Sharon Machlis Rain cloud plot produced with the ggdist plan. Take a look at the ggdist site for complete details and more examples. ggidst is by Matthew Kay and is available on CRAN.Add interactivity to ggplot2: plotly and ggiraph If your plots are going on the web, you may desire them to be interactive, using features like turning series on and off and displaying underlying information when mousing over a point, line, or bar. Both plotly and ggiraph turn ggplots into interactive HTML widgets.plotly, an R wrapper to the plotly.js JavaScript library, is extremely simple to utilize. All you do is location your last ggplot within the package’s ggplotly () function, and the function returns

an interactive variation of your plot. For instance: library (plotly) ggplotly( ggplot( snowfall2000s, aes( x= Winter season, y = Overall) )
+ geom_col( )+ labs( title=”Annual Boston Snowfall “, subtitle=” 2000 to 2016″ )) plotly deals with other extensions,

including ggpackets and gghighlights.plotly charts don’t always consist of everything that appears in a fixed version (as of this composing it didn’t recognize ggplot2 subtitles, for instance). However the plan is tough to beat for fast interactivity.Note that the plotly library also has a
non-ggplot-related function, plot_ly(), which uses a syntax comparable to ggplot’s qplot():
plot_ly( snowfall2000s, x= ~ Winter, y= ~ Overall
, type=” bar “). Source

Leave a Reply

Your email address will not be published. Required fields are marked *