R的新手,提前致歉。我已运行以下代码来获取单独的图,以显示每个Occupation_Proper的Injury_Areas的百分比。
SelectOccupation_InjuryAreas %>%
group_by(Occupation_Proper) %>%
mutate(prop = counnt / sum(counnt)) %>%
ggplot(aes(Injury_Area, prop)) +
ylab('prop') +
geom_bar(stat="identity") +
scale_y_continuous(labels=percent) +
facet_wrap(~ Occupation_Proper,ncol=2)+
geom_col(aes(fill = Occupation_Proper), position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_text(aes(label = scales::percent(prop),
y = prop,
group = Occupation_Proper),
position = position_dodge(width = 0.9),
vjust = 1.5)
我想分别显示每个Injury_Area占每个Occupation_Proper所有Injury_Area总数的百分比。由于某种原因,我运行的代码将Injury_Area百分比显示为所有地块的百分比,而不是分别显示每个地块的百分比(例如,每个地块的百分比应合计100),请参见图1的图片。。如何分别获得每个图的比例百分比?谢谢!
我的数据如下(使用R Studio,因此希望这是正确的显示方式。
structure(list(Occupation_Proper = c("HTO", "HTO", "HTO", "HTO",
"HTO", "HTO", "HTO", "HTO", "HTO", "HTO", "HTO", "HTO", "HTO",
"HTO", "HTO", "HTO", "HTO", "HTO", "Bl", "Bl", "Bl", "Bl", "Bl",
"Bl", "Bl", "Bl", "Bl", "Bl", "Bl", "Cb", "Cb", "Cb", "Cb", "Cb",
"Cb", "Cb", "Cb", "Cb", "Cb", "Cb", "Cb", "CrO", "EO", "EO",
"EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO",
"EO", "EO", "L", "L", "L", "TR", "TR", "TR", "TR", "TR", "TR",
"TR", "TR", "WH", "WH", "WH", "WH", "WH", "WH", "WH", "WH", "WH",
"WH", "WH", "WH"), Injury_Area = c("Back", "Neck", "Hand", "Head",
"Face", "Arm", "Leg", "Foot", "Knee", "Chest", "Eye", "Ear",
"Ribs", "Throat_Tongue", "Other", "Hip", "Buttock_Pelvis", "Torso",
"Neck", "Hand", "Head", "Arm", "Leg", "Knee", "Eye", "Ear", "Other",
"Hip", "Torso", "Neck", "Hand", "Head", "Arm", "Leg", "Foot",
"Knee", "Eye", "Other", "Hip", "Buttock_Pelvis", "Torso", "Knee",
"Neck", "Hand", "Head[", "Face", "Arm", "Leg", "Foot", "Knee",
"Torso_Buttock", "Eye", "Ear", "Spill", "Other", "Buttock_Pelvis",
"Torso", "Arm", "Eye", "Other", "Neck", "Hand", "Face", "Arm",
"Knee", "Eye", "Ear", "Hip", "Hand", "Head", "Face", "Arm", "Leg",
"Knee", "Eye", "Ear", "Other", "Hip", "Buttock_Pelvis", "Torso"
), counnt = c(4, 30, 20, 17, 15, 18, 32, 6, 13, 5, 12, 9, 2,
3, 50, 5, 6, 3, 2, 1, 1, 2, 7, 7, 1, 2, 2, 1, 1, 4, 7, 2, 5,
16, 1, 5, 1, 2, 3, 1, 2, 2, 14, 18, 6, 7, 19, 14, 3, 6, 1, 5,
4, 2, 26, 1, 2, 1, 1, 1, 1, 11, 2, 7, 1, 1, 5, 1, 12, 1, 3, 8,
2, 1, 2, 2, 2, 1, 1, 1)), row.names = c(NA, -80L), class = "data.frame")
df %>%
rename(count = counnt) %>%
group_by(Occupation_Proper) %>%
mutate(percent = (count/sum(count) * 100)) %>%
ggplot(aes(x = Injury_Area, y = percent, fill = Occupation_Proper)) +
geom_bar(stat="identity") +
facet_wrap(~ Occupation_Proper, ncol=2) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
geom_text(aes(label = paste(sprintf("%.1f", percent), "%", sep=""),
y = percent+7.5))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句