More for you to like:
Closed Captioning Closed captioning available on our YouTube channel
You Might Like

How to add color to your ggplot2 text in R

InfoWorld | Feb 19, 2020

See how to style your headlines, axis text, and more in your ggplot graphs with the R ggtext package. Download the data from Sharon's associated article: https://www.infoworld.com/article/3527449/add-color-to-your-ggplot2-text-in-r.html

Copyright © 2020 IDG Communications, Inc.

Hi. I’m Sharon Machlis at IDG Communications here with Episode 42 of Do More With R: Add color to text in ggplot2.

That’s one of the cool things I learned at RStudio Conference this year. And it’s a bit easier than I thought, thanks to the ggtext package. Let’s take a look.

I installed the dev version of ggplot2 from GitHub to make sure I had the super-latest version, since in general, some things demo’d at the conference aren’t on CRAN yet. And ggtext definitely does NOT work with some older versions of ggplot.

You have to install ggtext from GitHub, since as of this recording, it’s not on CRAN. I use the remotes package to install from GitHub; devtools does the same thing. Note in my code I include the argument build_vignettes = TRUE so I have package vignettes on my system.

Next I load ggplot2, ggtext, and dplyr into my working session.

For demo data, I’m going to use info about R tweets compared with Python tweets. I took a random sample of 1,000 of each, did some filtering, and then calculated how many in each group: have at least 5 likes, at least 5 retweets, include a URL, and include media like a photo or video.

If you’d like to follow along, you can download that small, summarized data frame from this video’s associated InfoWorld article. Or, use any data that has a few groups, a few categories and a few values, and makes sense for a stacked bar chart.

I’ll load my data. It’s in a “long” format: One column for the hashtag (#rstats or #python), one for the category I’m measuring, and then only one column with values. That’s just the structure you want for most ggplot graphs.

Here I’m making a basic grouped bar chart and saving it to a variable I’m calling my_chart. First line tells ggplot that I’m using the graph_data data frame, and I want the Category column on the x axis, number of tweets on the Y axis, and fill color to be based on Hashtag. Second line creates a column chart; position dodge groups the bars instead of stacking them. Alpha .9 just makes the bars a little transparent (1 is fully opaque).

The rest of the code customizes the look of the graph. I’m using the minimal theme, getting rid of X and Y axis labels, removing the default grid lines, and then setting colors for the bars. Now let’s see what that looks like. Basic graph with a legend.

Next I’ll add a title. Looks fine. But at a separate RStudio Conference session on general dataviz best practices, we were told making people look at a legend off to the side wasn’t ideal. Putting the color right in the graph headline can be better for the viewer. Let’s do that.

Knowing a little HTML styling with in-line CSS – that’s short for Cascading Style Sheets – will definitely help you customize your text. Here I’m using span tags to section off the parts of the text I want to affect – python and rstats. In the span tag I’m setting a style – specifically setting text color and then the hex value of the color I want. This is standard HTML styling, and there are loads of tutorials on the Web if you want brief explainers. You can also use available color names in addition to HEX values - although of course you want to make any changes it in both the headline and the graph. Let me undo that.

Note that there are two parts to this. In addition to adding my styling to the text, I need to add element_markdown() to whatever plot element has the colors. I do that here inside a theme() function.

I find it a little hard to see the colors in this headline text, though. I can add strong tags to make the text bold. I also added legend.position = none to remove the legend.

If I want to change the color of the x-axis text, I need to add that data to my graph data. In this code, I’m creating a column that will add bold italic red to the FiveLikes and FiveRTs category labels; and styles the rest as bold italic without the red color.

Now I need to re-create the full chart to use the new, updated data frame. This chart code is mostly the same as before but with 2 changes: My x axis is now the new category_with_color column. And I added element_markdown() to axis.text.x inside the theme() function.

If I run that hopefully you can see the changes in the x axis labels.

There’s more you can do with ggtext, such as creating stylized text boxes and adding images to axes. But package author Claus Wilke warned us at the conference not to go too crazy. Because everything you can do in R Markdown isn’t yet supported here. The package is under development – you can check out the website to see the latest, at wilkelab.org slash ggtext.

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 the YouTube IDG Tech Talk channel -- where you can subscribe so you never miss an episode. Hope to see you next time!
Popular
Featured videos from IDG.tv