Albert Rapp Profile picture
Sep 30, 2022 20 tweets 7 min read Read on X
BEFORE
- Standard bar chart
- No clue what the message is

AFTER
- Bar chart with nuanced color use
- Informs your reader about key insights & actions

The latter is actually pretty easy to pull off.

At the end of this step-by-step guide, you can do that too. #rstats #dataviz Image
Here's our starting point.

All code is available at albert-rapp.de/posts/ggplot2-…

Note that this tutorial is a ggplot2 recreation of Image
// Labels on y-axis

First, move the names to the y-axis.

This is important when the labels are real names instead of IDs.

No one likes to tilt their head for reading. Image
// Start with grey

Use the group aesthetic instead of fill.

Reserve colors for storytelling. Image
// Reference points

Add a reference point to put your data into perspective.

A simple average can be enough. Here, you see the average as the new category "ALL" Image
// Order top to bottom

Notice that I also ordered the bars from top to bottom? fct_reorder() did that for me.

This way, your reference point can do its job perfectly.

(Ordering is also a good idea even without reference points) Image
// Put important bars on the outside

I've also arranged bars with factor(). This way, important quantities are on the outsides (left and right).

This makes comparisons easier. Image
// Highlight with colors

Use colors to highlight your story points. Image
PRO TIP: Save your colors as variable names.

Usually, you will try out different colors.

Copy and pasting a new color into multiple parts of your code is tedious.
// Avoid clutter

Remove anything that clutters your plot.

This includes useless grid lines.

Do this by setting expand = FALSE in coord_cartesian() Image
Notice how I've also set clip = 'off'.

This allows drawing outside the grid panel.

We'll need that later.
// Move and format axes

Move x-axis to the top with scale_x_continuous().

This way, your readers can see it early on.

Also, grey out the axes labels with theme(). Otherwise, they can be distracting. Image
// Align labels

It's a good idea to move labels such that they form a clean line (e.g. with an axis)

Use theme() to move the axis titles to the top left corner.

BONUS: The reader can see the titles even sooner. Image
// Align manually

Not all things can be set in theme(). For example, the axes label can only use a single hjust value.

But "clear" lines need
0% label ➡️ left-justified
100% label ➡️ right-justified

An annotation outside the panel grid will do the trick. Image
// Label directly

Use geom_text() to label your highlights. Image
// Add a legend

We can add a small legend using another annotation outside the panel.

This will be a richtext annotation because we highlight a single word. Image
// Create a story plot

The bar plot is finished. Time to create the story texts.

Create a text-only plot first. We'll combine it with the bar plot later. Image
// Combing bar plot and story

Use {patchwork} to put together your two plots.

Add the call-to-action prompts via a new title + subtitle. ImageImage
Done! That's a wrap. 🥳

Hope you found this guide helpful. You can follow @rappa753 for more content like that.

And don't forget to jump back to the first tweet below to like or retweet the guide. I'd appreciate it 👌🏻
Want more helpful resources?

Every week, my newsletter shares insights on
- dataviz,
- Shiny web apps
- stats

Reading time: 3 minutes or less

You can join at
alberts-newsletter.beehiiv.com/subscribe

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Albert Rapp

Albert Rapp Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @rappa753

Sep 9, 2023
Three steps to use color in your title instead of wasting space on a huge legend. Image
1 // Wrap your subtitle into <span> tags

These span-tags are HTML notation for inline text. So in principle, adding them should change nothing.

But as you can see, it does have an impact.
Image
Image
2 // Enable HTML notation

The problem is that ggplot does not know that you want to use HTML notation.

So, enable that with element_markdown() from the {ggtext} package in theme.

This will render the span-tags instead of displaying them as text:
Image
Image
Read 7 tweets
Aug 26, 2023
Paired bar charts suck at comparing values. The only reason they're used all the time is because they are easy to create.

But there are better alternatives that are just as easy.

Here's how to create 4 better alternatives with #rstats. Image
0 // Where's the code?

The code for all plots can be found at

This thread walks you through the code quickly.albert-rapp.de/posts/ggplot2-…
1 // Dot plot

Instead of using bars next to each other, why not points on the same line?

Makes comparison suuper easy.

And it takes only a geom_point() layer. Dead-simple, right?

I think it's even easier to create than a paired bar chart.
Image
Image
Read 15 tweets
Aug 19, 2023
R makes it dead-simple to use some of the most effective dataviz principles.

Here are six principles that are so easy that any ggplot beginner’s course should teach them.
1 // Make sure your labels are legible

Too many plots use waaaay too small texts.
With ggplot, it just takes one line to fix this.

Img 1: Way too small fonts & unclear labels
Img 2: Fixed with labs() and theme_gray(base_size = 20)
Img 3: Full code

Image
Image
Image
2 // Use a minimal theme

As a rule of thumb, you should minimize everything that could potentially distract your audience.

That’s why I usually recommend to use a minimal theme: Just use `theme_minimal()` instead of `theme_gray()`. Image
Read 9 tweets
Jun 17, 2023
Need to extract days, months, years or more from time data?

Don't compute them all manually with {lubridate}. That's way too tedious.

The {timetk} package has a nice function that does all the heavy lifting for you.

LEFT: {lubridate} workflow
RIGHT: {timetk} workflow
#rstats ImageImage
BONUS: Maybe you don't want use all of the stuff that {timetk} computes for you.

Here's a simple function that extracts only the parts you want.

All of the code can be found on GitHub at gist.github.com/AlbertRapp/2c9… Image
Also, shoutout to @EatsleepfitJeff for teaching me about this function from {timetk} ☺️
Read 4 tweets
Jun 10, 2023
Everybody loves colors but only few know how to use them well.

With the right guidelines, using colors becomes super easy.

Let me show you how to implement these guidelines with ggplot 🧵
#rstats
Anyone can create a stacked bar chart with ggplot.

But that can end up in a colorful & messy plot.

Let's implement a couple of guidelines from this datawrapper blog post to level up our color game blog.datawrapper.de/10-ways-to-use… Image
The key is to reduce the amount of colors and leverage the `alpha` aesthetic as well. Image
Read 11 tweets
Jun 7, 2023
Sometimes people ask me if I can do one-on-one R tutoring.

Sure I can. But then my hourly rate applies. And there are many amazing *free* resources. Want to try them first?

Here are a few that I recommend. #rstats
1 // Yet Again: R + Data Science

Find it at yards.albert-rapp.de

I'll start with one of my own bc I assume that you like my style (otherwise why ask me?)

Beware though: YARDS is a graduate-level course that I taught for math students w/ a bit of programming experience. Image
2 // R for Data Science

To me this book is like the R bible. It introduced me to the so-called tidyverse and taught me much of what I know.

This one starts out slow and is really beginner-friendly

r4ds.had.co.nz
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(