今天的推文是回答B站关注者的一个问题
他的问题是
image.png
我找到论文来看了一下
image.png
图片是
image.png
今天的推文我们试着来复现一下这个图
首先是准备数据
没有找到论文提供的原始数据,只能手动将其整理到表格里了。
image.png
这里用qq里面的截图工具识别图片文字还挺方便的
完整代码
代码语言:javascript复制library(readxl)
library(ggplot2)
df<-read_excel("abcd.xlsx")
df
df$y<-factor(df$y,
levels = rev(unique(df$y)))
df$group<-factor(df$group,
levels = rev(unique(df$group)))
ggplot(data=df,aes(x=x,y=y,fill=group))
geom_bar(stat = "identity",
position = position_dodge(0.9),
width = 0.8)
scale_fill_manual(values = c("#4472c4","#ed7d31"))
geom_text(aes(x 0.2,y,label=x),size=3,
position = position_dodge(0.9))
theme_void()
theme(legend.position = "bottom",
legend.justification = c(0.5,0),
legend.title = element_blank(),
legend.key.size = unit(2,'mm'),
legend.text = element_text(size=10),
plot.margin = unit(c(1,1,2,1),'mm'))
geom_text(aes(x=-0.1,y=y,label=y),hjust=1)
xlim(-11,5)
geom_vline(xintercept = 0,color="grey")
annotate(geom = "segment",
x=0,xend=-11,y=0.4,yend=0.4,
color="grey")
annotate(geom = "segment",
x=0,xend=-11,y=2.5,yend=2.5,
color="grey")
annotate(geom = "segment",
x=0,xend=-11,y=5.5,yend=5.5,
color="grey")
annotate(geom = "segment",
x=0,xend=-11,y=12.5,yend=12.5,
color="grey")
annotate(geom = "segment",
x=0,xend=-11,y=16.5,yend=16.5,
color="grey")
annotate(geom = "text",
x=-11,y=1.5,label="Others",angle=90)
annotate(geom = "text",
x=-11,y=4,label="Provisioning",angle=90)
annotate(geom = "text",
x=-11,y=9,label="Cultural",angle=90)
annotate(geom = "text",
x=-11,y=14.5,label="Regulationg",angle=90) -> p
pdf(file = "outp.pdf",
width = 14,height = 6,
family = "serif")
print(p)
dev.off()
image.png
整体的思路就是Y轴的坐标轴标签去掉,用geom_text()
函数添加文本注释的办法作为标签,这样有了坐标位置添加横线表示分组就很方便了