R 如何使用ggplot2显示计数条形图,其中数据存储在多列中?
我有一个描述伤害患病率的数据集,我正试图对其进行初步分析 伤害=R 如何使用ggplot2显示计数条形图,其中数据存储在多列中?,r,ggplot2,R,Ggplot2,我有一个描述伤害患病率的数据集,我正试图对其进行初步分析 伤害= ID Injury.face Injury.neck Injury.chest Injury.pelvis Inj.loc.count 1 Checked Unchecked Unchecked Unchecked 1 2 Unchecked Checked Unchecked
ID Injury.face Injury.neck Injury.chest Injury.pelvis Inj.loc.count
1 Checked Unchecked Unchecked Unchecked 1
2 Unchecked Checked Unchecked Checked 2
3 Checked Unchecked Checked Unchecked 2
4 Unchecked Checked Checked Checked 3
5 Unchecked Unchecked Unchecked Checked 1
我希望使用ggplot2显示数据,以便在条形图中按位置查看受伤频率,并通过Inj.loc.count列对条形图进行分面
数据集是一个较大表的子集,有10个injury.X列。
Inj.loc.count是每行中“已检查”值数量的计数
我正在努力研究如何将每个伤害的计数显示为一个条形图
我在提交这篇文章之前的搜索结果都显示了在多个栏中显示的变量在一列中的文章
抱歉,如果我在格式设置方面出错,这是我的第一篇SO文章。在打印之前,请使用Reforme2库转换为正确的格式
reformatted <- melt(Injuries, id.vars = c("id")
重新格式化的tidyr方法是:
library(tidyr)
df <- structure(list(ID = 1:5, Injury.face = structure(c(1L, 2L, 1L,
2L, 2L), .Label = c("Checked", "Unchecked"), class = "factor"),
Injury.neck = structure(c(2L, 1L, 2L, 1L, 2L), .Label = c("Checked",
"Unchecked"), class = "factor"), Injury.chest = structure(c(2L,
2L, 1L, 1L, 2L), .Label = c("Checked", "Unchecked"), class = "factor"),
Injury.pelvis = structure(c(2L, 1L, 2L, 1L, 1L), .Label = c("Checked",
"Unchecked"), class = "factor"), Inj.loc.count = c(1L, 2L,
2L, 3L, 1L)), .Names = c("ID", "Injury.face", "Injury.neck",
"Injury.chest", "Injury.pelvis", "Inj.loc.count"), class = "data.frame",
row.names = c(NA, -5L))
new <- gather(df, key=c(ID, Inj.loc.count), Injury, -ID, -Inj.loc.count)
colnames(new) <- c("ID", "Inj.loc.count", "Name", "Injury")
> head(new, 10)
ID Inj.loc.count Name Injury
1 1 1 Injury.face Checked
2 2 2 Injury.face Unchecked
3 3 2 Injury.face Checked
4 4 3 Injury.face Unchecked
5 5 1 Injury.face Unchecked
6 1 1 Injury.neck Unchecked
7 2 2 Injury.neck Checked
8 3 2 Injury.neck Unchecked
9 4 3 Injury.neck Checked
10 5 1 Injury.neck Unchecked
# count checked/unchecked injuries
n <- new %>% group_by(Name, Injury) %>% count
# join n to main df by Name and Injury type
new_df <- new %>% left_join(n, by=c("Name", "Injury")) %>% mutate(Name = gsub("Injury.","", Name))
# plot the data, faceted by Inj.loc.count
ggplot(new_df, aes(x = Name, y = n, fill = Injury)) +
geom_bar(position = 'dodge', stat='identity') +
facet_wrap(~Inj.loc.count, ncol = 1)+
geom_text(aes(label=n), position=position_dodge(width=0.9),
vjust=1.5, color = "white", fontface = "bold") +
labs(y = "Number of cases")
library(tidyr)
df%突变(名称=gsub(“伤害”,“名称”)
#按Inj.loc.count绘制数据
ggplot(新的_df,aes(x=名称,y=n,填充=伤害))+
几何图形栏(位置='dodge',状态='identity')+
镶嵌面包裹(~Inj.loc.count,ncol=1)+
几何图形文字(aes(标签=n),位置=position_减淡(宽度=0.9),
vjust=1.5,color=“白色”,fontface=“粗体”)+
实验室(y=病例数)
问题中是否包括您的R代码?您指的是什么具体的R代码?计算inj.loc.count的代码?非常感谢。我利用了这个过程。这是我使用的代码:#重新格式化受伤数据表谢谢你的详细回答。我不熟悉tidyr,因此我将在将来再次讨论这个答案。最后,我使用Reforme2将数据转换成一种更易于管理的格式,然后我就能够输出一个可比较的输出。我使用了您的酒吧标签建议,因为这是一个添加细节的简洁解决方案。请记住,tidyr或dplyr fun的性能优于Reforme2功能。