准备工作 🔗
复现发表在nature的Europeans’ support for refugees of varying background is stable over time里的Extended Data Fig. 4。原图如下:
文献提供了原始数据集,下载到本地直接使用。set up R, 加载需要用到的R包:
xfun::session_info()
library(dplyr)
library(readxl)
library(ggplot2)
先看看数据 🔗
ef4 <- read_excel("E:/linyu/static/posts/2023_08_17refugee/data/ef4.xlsx")
tmp <- ef4 %>%
mutate(x_var = factor(x_levels, levels = as.character(1:45))) %>%
arrange(desc(x_var))
tmp1 <- tmp %>% filter(face_var == 'bold')
tmp2 <- tmp %>% filter(face_var == 'plain')
画图: 🔗
tmp %>%
ggplot(aes(x = Estimate, y = x_var,shape = face_var)) +
geom_vline(xintercept = 0,color = 'darkgrey',size = 1.1)+
geom_pointrange(aes(color = color_var,
xmin = Estimate - SE,
xmax=Estimate + SE),stroke = 2)+
facet_wrap(~facet_var)+
scale_y_discrete(name= "",
limits = rev(levels(tmp$x_var)))+
theme(plot.margin = unit(c(1, 1, 1, 10), "lines"))+
scale_shape_manual(
name = "",
values = c(1, 16),
breaks = c("bold", "plain"))+
theme(legend.position = 'none')+
theme(axis.text.y = element_blank())+
coord_cartesian(xlim = c(-0.2, 0.2),clip = 'off')+
scale_x_continuous(breaks = seq(-0.2,0.2,0.2))+
geom_text(data=tmp1,aes(x= - 0.53,label = y_labels),
hjust=0,
fontface = tmp1$face_var)+
geom_text(data = tmp2,aes(x = -0.5,label = y_labels),
hjust=0,
fontface = tmp2$face_var)+
theme(axis.ticks.y = element_line( size =1.3,color = 'darkgrey'),
axis.ticks.length.y = unit(.2,'cm'),
axis.ticks.x = element_blank())+
theme(panel.grid = element_line(color='grey'))+
theme(panel.background = element_rect(fill = "white"))+
theme(axis.line = element_line(color='black'))+
theme(panel.border = element_rect(color = "black", fill = NA, size = 1),
panel.spacing.x = unit(0.5,'cm'),
strip.background = element_rect(color = "black", fill = 'lightgrey', size = 1))
最后输出结果: