在 R 中创建 "The Economist" 样式图?

Create "The Economist" Style Plots in R?

这个问题有两个部分,一个比较笼统,另一个是具体案例:

  1. R 中是否有主题或模板用于生成与 "The Economist" 杂志中发布的图表具有相似外观的图表?其他上下文中的示例包括: for python and set scheme economist for Stata.

  2. 具体来说,生成类似于下面示例的组条形图的语法(例如,在 ggplot2 中)是什么,带有跨越范围的粗线的彩色形状标记它们之间(左图),或矩形置信区间(右图)?

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

是的,您在 ggthemes(ggplot2 的扩展)中有 theme_economisttheme_economist_white

对于条形图,您需要使用 geom_barcoord_flip (here)

ggthemes 文档中的示例 (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()

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

如何重现示例中给出的情节

由于我无法在我的电脑上安装SciencesPo包,我建议你使用ggplot + ggthemes的方法。

以下方法可能是一个很好的起点。我以 diamond 数据集为例。

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))

然后是剧情

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()