使用geom_bar显示多年来的2个二进制变量
我的数据集由4个变量和大约200k个完整的观测值组成使用geom_bar显示多年来的2个二进制变量,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我的数据集由4个变量和大约200k个完整的观测值组成 recordno:ID变量 年份:受伤年份 txa:二进制0/1,1表示给予药物 钝性损伤:钝性或穿透性损伤类型,二元 1) 我正在尝试使用ggplot2创建一个条形图,描述2013年至2019年间两种类型损伤的药物使用情况。问题是我的Y通道显示了错误的药物计数,因为每种类型的创伤的两个条都是最大的 2) 我还试图在每个盒子上贴一个标签来显示计数。(当年使用txa的次数) 谢谢你的帮助。请参阅下面的代码 utilize <- da
- recordno:ID变量
- 年份:受伤年份
- txa:二进制0/1,1表示给予药物
- 钝性损伤:钝性或穿透性损伤类型,二元
utilize <- data.frame(txa = c(0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1),
year = c("2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019", "2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019","2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019","2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019","2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014"),
blunt=c("blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt", "blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt", "blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt","blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt","blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt"))
util2 <- utilize %>%
group_by(year, blunt) %>%
summarise(txa_total = sum(txa))
ggplot(util2, aes(fill=blunt, y= txa_total, x=year, group = blunt)) +
geom_bar(stat="identity", color="black", position=position_dodge())+
geom_text(aes(label=txa_total), color="black",
size=3.5, position = position_dodge(width = 1),
vjust = -.25)+
ylim(c(0,2000)) +
theme(plot.title = element_text(size = 12, face = "bold"),
plot.caption = element_text(face= "italic"),
axis.title.x = element_text(size = 10, face= "bold"),
axis.title.y = element_text(size = 10, face= "bold"))+
scale_fill_manual(name = "Type of trauma", values=c("#E69F00", "#56B4E9"))+
labs(x = "Year of Injury",
y = "Count of cases receieving TXA",
title = "TXA Utilization in Trauma Patients in the State of Michigan Between 2013 and 2018",
subtitle= "Plot of number of trauma cases by year",
caption = "Data source: MTQIP 2013-2018")
利用%
总结(txa_总计=总和(txa))
ggplot(util2,aes(填充=钝性,y=txa_总数,x=年份,组=钝性))+
几何图形栏(stat=“identity”,color=“black”,position=position\u dodge())+
geom_文本(aes(标签=txa_总数),color=“黑色”,
尺寸=3.5,位置=位置减淡(宽度=1),
vjust=-.25)+
ylim(c(02000))+
主题(plot.title=element_text(size=12,face=“bold”),
plot.caption=element_text(face=“italic”),
axis.title.x=元素\文本(大小=10,面=“粗体”),
axis.title.y=元素\文本(大小=10,面=“粗体”))+
刻度填充手册(名称=“创伤类型”,数值=c(“#E69F00”),“#56B4E9”))+
实验室(x=“受伤年份”,
y=“接收TXA的病例数”,
title=“2013年至2018年间密歇根州创伤患者的TXA使用情况”,
subtitle=“每年创伤病例数量图”,
标题=“数据来源:MTQIP 2013-2018”)
我认为您需要转换数据,使之更容易。如果您正在寻找利用率(给定/总计),但应该很容易更改汇总调用
utilize <- data.frame(txa = c(0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,1),
year = c("2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019", "2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019","2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019","2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014", "2019","2013","2016","2017", "2019", "2013", "2017", "2018", "2015", "2014"),
blunt=c("blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt", "blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt", "blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt","blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt","blunt","penetrating","blunt","blunt","penetrating","blunt","penetrating","blunt","penetrating","blunt"))
util2 <- utilize %>%
group_by(year, blunt) %>%
summarise(txa_total = sum(txa))
ggplot(util2, aes(fill=blunt, y= txa_total, x=year, group = blunt)) +
geom_bar(stat="identity", color="black", position=position_dodge())+
geom_text(aes(label=txa_total), color="black",
size=3.5, position = position_dodge(width = 1),
vjust = -.25)+
theme_bw()+
labs(x = "Year of Injury",
y = "Cases where TXA was administered",
title = "Utilization of TXA in trauma patient between 2013 and 2019"
)
use%
总结(txa_总计=总和(txa))
ggplot(util2,aes(填充=钝性,y=txa_总数,x=年份,组=钝性))+
几何图形栏(stat=“identity”,color=“black”,position=position\u dodge())+
geom_文本(aes(标签=txa_总数),color=“黑色”,
尺寸=3.5,位置=位置减淡(宽度=1),
vjust=-.25)+
主题_bw()+
实验室(x=“受伤年份”,
y=“使用TXA的病例”,
title=“2013年至2019年间创伤患者使用TXA的情况”
)
谢谢您的代码。我试过了,但有个小问题。并非所有x轴标签都显示。我在上面调整了代码。谢谢