Closed Captioning Closed captioning available on our YouTube channel

How to make easier ggplot2 with ggcharts

InfoWorld | Mar 3, 2022

Make ggplot2 graphs with a lot less code using the ggcharts package.

Copyright © 2022 IDG Communications, Inc.

Similar
Hi, I’m Sharon Machlis at InfoWorld, here with Episode 69 of Do More With R: Easy, intuitive dataviz with the ggcharts package.
ggplot2 is one of the most popular R packages for visualizing data – or, really, for anything at all. It’s very powerful, well thought out, and allows a ton of customizations. But it does have a learning curve, and there are people who find some of its functions hard to remember. If you want to create a bar chart or line graph that's report-ready right out of the box – quickly and easily -- ggcharts may be for you. Let’s take a look.
Here’s a bar chart of high temperatures in Boston last work week. (Wednesday was amazing, by the way). And this is my ggplot2 code that made the graph. Now here’s a ggcharts graph with the same data and here’s the ggcharts code I used.
To be fair, I didn’t need all the customization I added to the ggplot version. But I generally don’t like the ggplot defaults.
ggcharts is a wrapper package for ggplot2. It only does a small subset of what ggplot is capable of. But as this overview shows, the R objects you create with ggcharts are also ggplot objects. And that means you can add ggplot customization code if you want to tweak your results. That can give you the best of both worlds – as long as you’re making one of the half dozen or so types of visualizations included in the package, that is.
One thing about the ggcharts default: It assumes you want to sort the results by value as opposed to keeping the x-axis in a specific order. A lot of times that is what you want, and it takes some extra code to reorder by y value in ggplot. With ggcharts, if you don’t want that sorting, just use the argument sort = FALSE.
The column_chart() function is for vertical bars, bar_chart() is for horizontal bars, otherwise as far as I can tell they’re pretty much the same. If you run help, you can see the various arguments and some more explanation for those arguments. Note that dot-dot-dot on line 5. You can add in any ggplot aes() argument too.
ggcharts’ highlight argument is useful. Here you can choose one or more x values to highlight visually on your graph. You can do that with ggplot also, of course, but I find it a bit more complicated. (I usually end up adding a new column to my data with specific color info I want). Now let’s say this is for a graph where the data changes, and you want to highlight “the highest y value” and not by a specific x value. Easy. I calculate what x is for the maximum y value, save that to a variable (in this case maxday), and then use that variable name with the highlight argument.
If you want to change the highlight color and other colors, you can use the highlight_spec() function to define what gets highlighted, the highlight color, and the other color.
If you know ggplot, you can add more customization to your ggcharts graph like I did here. This adds a title and subtitle, sets the plot title and subtitle font size, and centers them. I also used ggplot’s geom_text() function to add labels to the bars.

Here’s a look at the chart types built into ggcharts. As I said before, it’s a small subset of what ggplot2 can do – but still useful if you want to make a quick, shareable graph using one of these formats. Bars, lines, lollipops, pyramids, and dumbbells. There are also a few built-in themes.

The package comes with a few built-in data sets. Here’s a look at one: biomedicalrevenue with 3 columns: company, year, and revenue. This is a basic lollipop chart with that data, first filtering the data set for just the latest year, and then using the top_n argument to just show the top 10 companies. To facet by a variable, in this case year, use the facet argument. And like with bar graphs, you can use the highlight argument to call out specific companies.

Here's a quick look at one of the themes, using the theme_coffee() function.

I think this is a pretty convenient way to start off doing basic visualizations in R. Especially since you can always customize with ggplot functions too.

To make things even simpler, you can combine ggcharts with the ggeasy package. ggeasy offers a user-friendly way to tweak things like axis text. Load the package and start typing easy, underscore, and something you’re looking for, like y axis, and you’ll see a drop-down menu of function choices in RStudio. Here’s how I’d change the y-axis text size by adding ggplot2 code. I don’t always remember this. And here’s how to do it with ggeasy. I covered ggeasy in another video; if you’re watching on YouTube, the link will be in this video’s description.

As long as we’re talking about easy ways to generate ggplot graphics, I don’t want to forget about esquisse! This R package offers drag-and-drop ggplot – and generates R code you can use in your scripts. I covered esquisse in another video; if you’re watching on YouTube, check for that link in the video description, too.

That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R. You can also find the Do More With R playlist on YouTube’s IDG Tech Talk channel where you can subscribe so you never miss an episode. Hope to see you next time. Stay healthy and safe, everyone!
Popular
Featured videos from IDG.tv