在 R 中创建“经济学家”风格的图?

dze*_*zer 5 plot r bar-chart ggplot2 stata

这个问题有两个部分,一个是一般性的,另一个是具体的案例:

  1. R 中是否有一个主题或模板来生成与“经济学人”杂志上发布的图表具有相似外观的图?其他上下文中的示例包括:从 python为 python 和set scheme economistStata创建“经济学人”样式的图形

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

在此处输入图片说明

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

lin*_*nog 5

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

对于条形图,您需要使用geom_barcoord_flip此处

来自 ggthemes 文档的示例(此处

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

## White
p + theme_economist_white() +
  scale_colour_economist()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

由于我无法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))
Run Code Online (Sandbox Code Playgroud)

然后剧情

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

Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明