Giter VIP home page Giter VIP logo

theme_fivethirtyeight's Introduction

fivethirtyeight.com ggplot2 theme

ggplot2 theme that mimics the style of fivethirtyeight.com (538) plots.

The repository contains the following files:

  • theme_fivethirtyeight.R: This is the actual theme function which you need to add to your project
  • example.R: A simple example making use of the 538 theme

You can install the theme with source("theme_fivethirtyeight.R"). Plots created with the 538 ggplot2 theme will look as follows:

Below you will find some general discussions which led to the theme design decisions. In addition, we highlight some aspects which cannot be modified with the ggplot2 theming system but which should be changed in order to make your plots even more 538-like.

Note: This little project was inspired by Austin's (Clemens) blog post of 538 style graphs in ggplot2. In his blog he modifies the theme object rather than defining a new theme function.

Reviewing 538 plots

538 (time series) plots are not as consistent as you might think which gets obvious by taking a look at the three examples below:

Example 1:

Example 2:

Example 3:

In the first two examples the x-axis is emphasized but in two different ways. First, with a bold line colored in dark grey. Next, with a very thin black line. The third plot is not showing such a style element at all.

The same is true for axis labels. Although most of the times they are not used, you will find cases in which either one axis is labeled (see Example 2) or both. However, 538 plots try to omit axis label usage as much as possible especially in time series plots.

Deciding on the theme design

We decided the following when creating the ggplot2 538 theme function:

  • x-axis is not highlighted
  • x- and y-axis labels are omitted (but can be added later to the theme object)

Although fonts could be set in the theming function, we are not leveraging the two standard 538 fonts here because the are not available for free:

  • Atlas Grotesk
  • Decima Mono

Building the theme function

In general, ggplot2 provides two ways for writing user-defined theme functions which are described in the ggplot2 themes vignette:

  • Write an entire new theme function in the style of theme_grey
  • Use the %+replace% operator which is primarily used to create new themes

Here, we chose the second option by deriving the 538 theme from theme_grey

Regardless in which way you create a new theme function, it is crucial that you understand ggplot's theme inheritance structure which looks as follows:

For example, once a theme element was set to element_blank() somewhere upstream the inheritance branch, a specific setting of a child's properties in the theme object or in a derived theme function will have absolutely no effect.

For instance, take a look at the following theme object modification of the standard ggplot2 theme theme_grey:

library(ggplot2)
ggplot(diamonds, aes(carat, price)) + geom_point() +
theme(axis.line.x = element_line(colour = 'red'))

The x-axis won't show up in red because axis.line is set to element_blank() in theme_grey. If you wish to highlight just the x-axis in red you have add the following modifications which respect the inheritance structure:

theme(axis.line = element_line(colour = 'red'),
      axis.line.y = element_blank())

Limitations of the 538 theme function

In addtion to calling the 538 theme you have to make use of scale_x_continuous and the scale_y_continuous function in order to make your plot even more 538-like (Take a look at example.R).

In general, all 538 time series plots start drawing the first x-axis and the first y-axis line in the left corner of the plot. This is not always the case with usual ggplot2 plots.

Therefore, you have to figure out the limits of the x-axis and y-axis values upfront and set the breaks attribute accordingly in the scale_[xy]_continuous call.

Moreover, the following 538 plot elements cannot be replicated by the ggplot2 theming system:

  • The plot footer which inlcudes the 538 logo and the data source
  • The subtitle using a smaller fonts than the main plot title

theme_fivethirtyeight's People

Contributors

alex23lemm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.