Albert Rapp Profile picture
πŸŽ“ Math PhD student & freelancer πŸ‘¨β€πŸ« Bite-sized insights on dataviz, web dev & data science with R at https://t.co/M34b5BzHTD

Jun 10, 2023, 11 tweets

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…

The key is to reduce the amount of colors and leverage the `alpha` aesthetic as well.

Once you have fewer colors. You can concentrate on a better choice. How about the color-blind safe okabe ito palette?

While you're at it. How about a better range of `alpha` values so that top and bottom blocks have the same transparency?

Now comes the hard part. Consolidate the two legends into one.

Overwriting the aesthetics in a guides() layer will do the trick.

There you go. Same information as in the beginning but less of a rainbow mess. 🌈

Still it would be nice to make adjacent blocks stand out.

No problem. Use the `color` aesthetic to add lines.

But wait. There is more πŸ₯³

Spaghetti plots are easy to create but hard to decipher.

This is a chance to shine for the {gghighlight} package.

{gghighlight} makes it dead-simple to emphasize parts of your plot.

And with a small annotation, we can immediately tell a story.

Of course, this also works with more than just one color.

Once we get rid of too many colors, why not also get rid of other clutter like the legend?

That's some efficient use of space and colors right there!

Finally, let's come full circle and apply the same labeling techniques to our initial bar plot.

That's a wrap πŸ₯³

For the full code and more explanations, check out my blog post at albert-rapp.de/posts/ggplot2-…

Let me know if you enjoyed this thread, by hitting the like button on the start of this thread below. ☺️

For more posts like this, follow @rappa753

See you next time πŸ‘‹

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling