We are R-Ladies Profile picture
Nov 29 14 tweets 11 min read
Day 2: Building your very own ggplot theme.

Here are three reasons why I think you should do this:
- Help orient your readers with text hierarchy
- Give everything some space to breathe
- Achieve effortless consistency with one extra line of code

Sound good? Let's dig in!
My starting point for creating a custom theme is typically theme_minimal(). It has sensible defaults such as relative text size and margins that we can build on, by just replacing some elements.

plot +
theme_minimal() +
theme(customise here)

Here's our plot with theme_minimal()
Text hierarchy is one of those things it's so much easier to demonstrate than to explain. Take a look at this image. (© Chaosamran_Studio)

What it demonstrates is that the way we format our text guides readers as to what's most important and what we don't mind them skipping.
What goes into text hiearchy? Three things that we can easily change within a ggplot theme:

1) Text colour. Pick as many variants on your text colour as you think you need. Here, I've used two, based on the dark text colour of my website (which is based on the blue/grey strip)
To create those variants, I used {monochromeR}, going darker from my website's blue/grey colour to create a dark text colour, and going lighter from there to create some lighter text colours.
2) Fonts. Picking fonts can be really tricky, but there are some really great resources out there (see below, where we're back to our "let others help you" mantra!).

Here, I've simply applied the fonts from my own website, changing the family element of element_text().
3) Text size. You can manipulate text size within theme() either by setting absolute sizes (e.g. size = 16), or relative sizes (e.g. size = rel(1.2)).

The relative size is a good idea if you're going to reuse this theme: change the base size as needed and everything follows!
At this point, we've done most of the work, but we can still make our data story easier to take in by giving everything a bit more space to breathe.

First, let's move the legend to reduce unnecessary eye movements, fade the grid, and remove an unnecessary axis title.
And now let's change the base size, increase the default line height and add margins around the text elements and around the plot itself, using the acronym TRouBLe to remember the margin values go Top, Right, Bottom, Left (or clockwise from the top).

Tada!
Finally, let's package this up as a function we can easily reuse to create consistency across our project by just adding `+ theme_rladiesdemo()` to every plot.

Even with no effort to reconcile colours (see yesterday's thread for tips on that), it has a big impact!
And now for some resources (1/3):
- Font advice: @glyphe. Oliver runs a great weekly newsletter with font recommendations, and has some really practical resources on his site, including a free webinar from today which should be available to replay later.
pimpmytype.com/font-choice/
@glyphe Resources (2/3)
- Fonts specifically within #dataviz, we're back to @datawrapper: blog.datawrapper.de/fonts-for-data…
- Font troubleshooting for #rstats (and other fun stuff such as registering variants!), this blog post by @yjunechoe: yjunechoe.github.io/posts/2021-06-…
@glyphe @Datawrapper @yjunechoe - To view what each change to the theme does to the plot, flick through my @quarto_pub slides on building a custom theme: cararthompson.github.io/nhsr2022-ggplo…

(As an aside, I love how easy it is to create these step-by-step demos using Quarto!)
@glyphe @Datawrapper @yjunechoe @quarto_pub Over to you! What are your favourite fonts (and font pairs!) to use in #dataviz? Or do you have any font horror stories to share?

Would be great to see some examples of how you've added text hierarchy to your data visualisations!

• • •

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

Keep Current with We are R-Ladies

We are R-Ladies 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 @WeAreRLadies

Nov 30
Day 3: Writing functions to create parameterised graphs

So, we've picked our colours and set up a nice theme. We already know we want to apply this to all our plots but what if we could reuse the same plot code across different bits of the data?

Easy, let's make a function! ImageImageImageImage
First, let's set up our plot. We're going to plot the number of penguins from each species within our dataset. We're using ggchicklet::geom_chicklet(), an anchor colour which is a blend of the blue and purple I mentioned on Day 1 and theme_rladiesdemo() which we built on Day 2. ImageImageImage
Now let's add some labels, so we can state the number of penguins in each species, along with the mean body mass.

To do this, I'm using my go-to #rstats annotation package {ggtext}, which allows us to apply some CSS to create the text hierarchy principles from Day 2. ImageImage
Read 14 tweets
Nov 29
2) Fonts. Picking fonts can be really tricky, but there are some really great resources out there (see below, where we're back to our "let others help you" mantra!).

Here, I've simply applied the fonts from my own website, changing the family element of element_text().
3) Text size. You can manipulate text size within theme() either by setting absolute sizes (e.g. size = 16), or relative sizes (e.g. size = rel(1.2)).

The relative size is a good idea if you're going to reuse this theme: change the base size as needed and everything follows!
At this point, we've done most of the work, but we can still make our data story easier to take in by giving everything a bit more space to breathe.

First, let's move the legend to reduce unnecessary eye movements, fade the grid, and remove an unnecessary axis title.
Read 9 tweets
Nov 28
Day 1: Setting up a bespoke colour scheme 🎨

🤫 I'm going to let you in on a secret... I find picking colours really tricky! Thankfully, I've found few ways round that.

My top tip is to let others help you! But first, a broad principle...
When picking colours for story telling, I try to make the colours as intuitive as possible.

Here's the adventure I took the Palmer Penguins on in a recent talk involving the #GreatPenguinBakeOff. See if you can guess the details. (The next tweet should give you a few clues!)
It's not about making your plots into a guessing game. It's about reducing cognitive load by making it easy to remember what's what.

And this allows me to illustrate one way to let others help you: photos! All of the colours in the previous plots were taken from these photos. Photos used to pick the colours in the bar charts above. Alt
Read 13 tweets
Nov 28
Hi folks! I'm Cara, an Edinburgh-based freelance data consultant, and I'm excited to be bringing you a week of #rstats content based around the things I enjoy creating the most: data visualisations and "enhanced" reproducible outputs.
Over the course of the week, we'll be exploring the different components that go into making a nice "branded" parameterised report, each day building on the days before until we get to our finished product: a parameterised celebration of RLadies within the NHS.

Here's the menu:
- Day 1: Setting up a colour scheme
- Day 2: Building a custom ggplot theme
- Day 3: Writing functions to create parameterised graphs
- Day 4: Manipulating text with R
- Day 5: A worked example of designing a pdf template (and a case for PDFs)
- Day 6: Our finished product!
Read 5 tweets
Nov 11
Today I'd like to share how I debug R code, highlighting some neat RStudio tools and tricks. It's not a linear process so this thread isn't exactly in order. 1/n
It may sounds silly, but first, I take a breath & *fully* read the error/warning message. The breath is because it may not be the first error of the day & I might be getting frustrated. The careful reading is because I want all available information before moving ahead. 2/n
I think about if I've seen the error before. My most common errors are easily fixed & include

object 'X' not found -> I forgot to load X or misspelled it

unexpected ')' -> I have an extra ) somewhere. Also for ] and }

3/n
Read 11 tweets
Nov 10
This morning, I'll share some tips for my favorite thing to do in R: make plots! I'm firming in the ggplot world so many tips are related to this and related packages.
So not surprisingly, my first tip is to checkout ggplot! I think the most powerful aspect it it's legends as they are created automatically and change as you change the plot. No fear of an incorrect legend!

Here is a great intro to ggplot in R
carpentries-incubator.github.io/introduction-t…
ggplot is in the tidyverse & thus, works seamlessly with dplyr, tidyr, etc. If I need to transform data *just* for a plot, I use pipes (%>%) to avoid saving the data and cluttering up my environment. For example:

my_data %>%
filter( ) %>%
ggplot( ) ...
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!

:(