README.md

FiveThirtyPlot

This repo contains the functions of the fivethirtyplot package, which once installed locally, provides helpful functions for creating and exporting graphics made in ggplot in the style used by the FiveThirtyEight data team.

This package is an unabashed clone of the BBC New's teams amazing bbplot package. I have only barely tweaked it to the FiveThirtyEight style. Even this README is the same. Seriously, it's the same package.

Installing fivethirtyplot

fivethirtyplot is not on CRAN, so you will have to install it directly from Github using devtools.

If you do not have the devtools package installed, you will have to run the first line in the code below as well.

# install.packages('devtools')
devtools::install_github('kiernann/fivethirtyplot')

Using the functions

The package has two functions for plots: style_538() and finalize_plot.

Detailed examples on how to use the BBC's version of this package to produce graphics are included in the BBC R cookbook, as well as a more general reference manual for working with ggplot2.

A basic explanation and summary here:

style_538()

style_538(): has no arguments and is added to the ggplot chain after you have created a plot. What it does is generally makes text size, font and colour, axis lines, axis text and many other standard chart components into FiveThirtyEight style, which has discerned by studying the graphics published by FiveThirtyEight by an outside perspective.

Examples of such style can be found in the article The 45 Best Charts We Made In 2018. Below is an example of a histogram plot, from which much of this style was initially based.

movie_ratings

The function is pretty basic and does not change or adapt based on the type of chart you are making, so in some cases you will need to make additional theme arguments in your ggplot chain if you want to make any additions or changes to the style, for example to add or remove gridlines etc. Also note that colours for lines in the case of a line chart or bars for a bar chart, do not come out of the box from the style_538 function, but need to be explicitly set in your other standard ggplot chart functions.

Example of how it is used in a standard workflow:

library(ggplot2)
histogram <- 
  ggplot(data = diamonds, mapping = aes(x = price)) +
  geom_histogram(fill = "#ed713a", bins = 10) +
  geom_vline(xintercept = median(diamonds$price), colour = "#222222") +
  style_538()

finalize_plot

finalize_plot: will save out your plot with the correct guidelines for publication for a BBC News graphic. It is made up of functions that will left align your title, subtitle and source, add the BBC blocks at the bottom right and save it to your specified location. The function has six arguments, three of which need to be explicitly set and three that are defaults unless you overwrite them.

Here are the function arguments:

function(plot_name,
         source_name,
         save_filepath = file.path(),
         width_pixels = 640,
         height_pixels = 450,
         logo_image_path = file.path(system.file("data", 
                                                 package = 'fivethirtyplot'),
                                     "placeholder.png"))

Example of how the finalize_plot() is used in a standard workflow. This function is called once you have created and finalized your chart data, titles and added the style_538() to it (see above):

finalize_plot(plot_name = histogram,
              source = "Source: ggplot2::diamonds",
              save_filepath = "filename_that_my_plot_should_be_saved_to-nc.png",
              width_pixels = 640,
              height_pixels = 550)


kiernann/fivethirtyplot documentation built on May 14, 2019, 11:06 p.m.