Global burden of lung cancer attributable to ambient fine particulate matter pollution in 204 countries and territories, 1990–2019
image.png
一位公众号读者留言问到下图的实现方法
这个图涉及到3个知识点
这三个知识点分成2期推文分别来介绍,今天的推文介绍堆积柱形图
部分数据如下
image.png
上图的横坐标轴看起来是离散的,但是我们用连续的数值来做横坐标,是为了后续再同一个图上叠加折线图更方便。
这里还有表示分组的标签我是用ABCD来代替,这样图例的前后顺序就固定了,我们不用在调整,如果表示实际分组的内容不是ABCD,我们可以后续更改图例的文字标签,我认为这样比直接用表示分组的实际文字可能会方便一点
library(ggplot2)
library(readxl)
dat01<-read_excel("example-1.xlsx",
sheet = "Sheet1")
ggplot()+
geom_bar(data=dat,
aes(x=x,y=y1,fill=group),
position = "stack",
stat="identity")
image.png
ggplot()+
geom_bar(data=dat,
aes(x=x,y=y1,fill=group),
position = "stack",
stat="identity")+
scale_fill_manual(values = c("#2271b6","#6bafd6",
"#9ecbe2","#d7e3ef",
"#cb181c","#fb6a4b",
"#fd9272","#fee1d3"))
image.png
ggplot()+
geom_bar(data=dat,
aes(x=x,y=y1,fill=group),
position = "stack",
stat="identity")+
scale_fill_manual(values = c("#2271b6","#6bafd6",
"#9ecbe2","#d7e3ef",
"#cb181c","#fb6a4b",
"#fd9272","#fee1d3"))+
theme_bw()+
labs(x="Age",y="Numbers of deaths")
image.png
ggplot()+
geom_bar(data=dat,
aes(x=x,y=y1,fill=group),
position = "stack",
stat="identity")+
scale_fill_manual(values = c("#2271b6","#6bafd6",
"#9ecbe2","#d7e3ef",
"#cb181c","#fb6a4b",
"#fd9272","#fee1d3"))+
theme_bw()+
labs(x="Age",y="Numbers of deaths")+
scale_y_continuous(breaks = seq(-35000,35000,5000),
labels = abs(seq(-35000,35000,5000)))+
scale_x_continuous(breaks = 1:15,
labels = unique(dat01$xlabel),
expand = c(0,0),
limits = c(0.3,15.7))
image.png
示例数据和代码会在下期推文给出获取方式