Albert Rapp Profile picture
Sep 30 20 tweets 7 min read
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 19
I hate it when I can't customize each part of my ggplot. Usually, that happens when an aesthetic is not vectorized.

Luckily, there's an easy workaround. Here's how. 🧵 #rstats
Take a look at these two curved lines. Currently, both have the same curvature.

But what happens if I want different curvatures? Image
Unfortunately, I can't just pass 2 numbers to curvature. This will give me an error 😢 💔 Image
Read 9 tweets
Aug 19
With #rstats, it's dead-simple to implement logistic regression or Poisson regressions. Or any other kind of generalized linear model.

Here's how you can do that with {stats} or with {tidymodels}. 🧵
#Statistics #MachineLearning
Need to brush up on the math behind these models before we get started?

My most popular thread may help you.
One more hint before we start:

All of my code examples can be copied from my newest blog post.

The data that I use here comes from {palmerpenguins}. And we're going to classify a penguin's sex based on its weight, species and bill length. 🐧 🐧

albert-rapp.de/posts/14_glms/…
Read 18 tweets
Aug 14
This is a🧵🧵 about small steps you can take to learn #dataviz.

It's mainly a collection of things I wish I had known when I started learning. (And it comes with many resources)
I used to get stuck trying to create something "big". But building a dataviz from scratch is waaay too much effort at first.

Learning just 1 new trick, that's doable.

Baby steps. They're not as exciting as a flashy new viz. But they're motivating bite-size chunks of success.
1 // Data

First you need data.

Don't overthink what may be exciting or not. Just grab this week's #tidyTuesday data set.

(There are advantages of using TidyTuesday data. I'll come to that next) github.com/rfordatascienc…
Read 14 tweets
Aug 11
Forget regular heat maps. Use bubbles on a grid instead 🔵 🟢 🤯

A short #dataviz thread 🧵 🧵

#rstats #ggplot2

1/8 Image
Regular heat maps have the crucial flaw of not showing how much samples were used. 🤔

That's totally fine when the different sizes are shown (e.g. with colors). It's what I did with my calendar plot a while back (special heat map)


2/8
But look what happens when I use a color gradient for a summary statistic.

Here, I try to show a relationship between sale price of a house and a property's size + location.

This looks right. But it isn't. Some medians were estimated with ridiculously small samples. 😱 💔

3/8 Image
Read 9 tweets
Jul 27
💎💎🧵🧵
5 hidden gems from well-known #rstats packages to spice up your #dataviz game.

(with many code examples from the R twitter community)
1 // Bump charts

With {ggbump} it's easy to show rankings over time. Most of the heavy-lifting comes from its geom_bump() function.

Here's a nice example from @steodosescu.

Clearly, geom_bump() is the star of this package. But don't ignore its lesser-known helper functions.
1b // geom_sigmoid()

The smooth curved lines in bump charts are powered by geom_sigmoid().

You would think that this function is only a minor character in {ggbump} but NOOOOO! It's a hidden gem 💎.

Check out how @geokaramanis used it to create a stunning visual. 🤯
Read 19 tweets
Jul 10
The #rstats ecosystem makes splitting a stacked bar plot simple. 🥳 This way, comparing groups is sooo much easier! 👌🏽

✂️ Split stacked bars with facet_wrap()
🪢 Combine splits with totals via {patchwork}

Code: gist.github.com/AlbertRapp/cd8…

Details in thread 🧵
#dataviz
I picked up this trick of splitting bar charts from "Better Data Visualizations" by Jonathan Schwabish. amzn.to/3AEE4DB

I haven't finished the book yet but it contains many great nuggets of dataviz wisdom from page 1.

Now, let's implement this trick in ggplot.
The stacked bar plot can be created with geom_bar(). You will need to map the car classes to `fill`.

Here, I have used the mako color palette from {viridisLite}. I learned about this beautiful color palette from @c_gebhard today.
Read 9 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 on Twitter!

:(