Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create "The Economist" Style Plots in R?

This question has two parts, one more general and the other a specific case:

  1. Is there a theme or template in R for producing plots that have similar appearance to the charts published in "The Economist" magazine? Examples in other contexts include: Create "The Economist" style graphs from python for python and set scheme economist for Stata.

  2. Specifically, what would be the syntax (e.g., in ggplot2) to produce a groups bar plot that would look like the example below, colored shaped markers with bold lines spanning the range between them (left panel), or rectangular confidence intervals (right panel)?

enter image description here

Source: https://www.economist.com/graphic-detail/2020/04/01/covid-19-may-be-far-more-prevalent-than-previously-thought

like image 279
dzeltzer Avatar asked Apr 12 '20 09:04

dzeltzer


1 Answers

Yes you have it in ggthemes (extension of ggplot2) with theme_economist and theme_economist_white.

For the bar plot, you will need to play with geom_bar and coord_flip (here)

Examples from ggthemes doc (here)

library("ggplot2")
library("ggthemes")

p <- ggplot(mtcars) +
     geom_point(aes(x = wt, y = mpg, colour = factor(gear))) +
     facet_wrap(~am) +
     # Economist puts x-axis labels on the right-hand side
     scale_y_continuous(position = "right")

## Standard
p + theme_economist() +
  scale_colour_economist()

enter image description here

## White
p + theme_economist_white() +
  scale_colour_economist()

enter image description here

How to reproduce the plot given in example

Since I cannot install SciencesPo package in my computer, I propose you a ggplot + ggthemes approach.

A good starting point might be the following approach. I use as an example the diamond dataset.

library(dplyr)
library(ggplot2)
library(ggthemes)

df <- diamonds %>%
  group_by(cut) %>%
  summarise(mean = mean(price), sigma = sd(price),
            n = n())
df <- df %>%
  mutate(int_minus = mean - 1.96*sigma/sqrt(n),
         int_plus = mean + 1.96*sigma/sqrt(n))

And then the plot

ggplot(df) +
  geom_segment(aes(x = int_minus, xend = int_plus, y = factor(cut), yend = factor(cut)), size = 2L, alpha = 0.4) +
  geom_point(aes(x = mean, y = factor(cut)), shape = 15, color = "blue", size = 4L) +
  theme_economist_white()

enter image description here

like image 99
linog Avatar answered Oct 16 '22 12:10

linog