So I currently have my data which looks like this.
structure(list(Reportable = c("Non-Reportable Injury", "Reportable Injury",
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury",
"Reportable Injury", "Non-Reportable Injury", "Reportable Injury",
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury",
"Reportable Injury", "Non-Reportable Injury", "Reportable Injury",
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury",
"Reportable Injury", "Non-Reportable Injury", "Reportable Injury",
"Non-Reportable Injury", "Reportable Injury"), Result_Description = c("OTHER",
"OTHER", "GROUND", "GROUND", "DOOR",
"DOOR", "STAIR STEP", "STAIR STEP", "FLOOR", "FLOOR", "CHAIR/SEAT",
"CHAIR/SEAT", "LOCOMOTIVE, OTHER", "LOCOMOTIVE, OTHER", "BALLAST, STONES, ETC.",
"BALLAST, STONES, ETC.", "DOOR, TRAP - PASSENGER TRAIN", "DOOR, TRAP - PASSENGER TRAIN",
"PLATFORM", "PLATFORM", "TRACK (RAIL)", "TRACK (RAIL)"), count = c(69L,
235L, 27L, 91L, 29L, 44L, 19L, 39L, 14L, 38L, 15L, 31L, 21L,
25L, 16L, 27L, 15L, 22L, 8L, 25L, 4L, 26L), pct = c("23%", "77%",
"23%", "77%", "40%", "60%", "33%", "67%", "27%", "73%", "32%",
"66%", "45.7%", "54.3%", "37%", "63%", "41%", "59%", "24%", "76%",
"13%", "87%"), total = c("69 (23%)", "235 (77%)", "27 (23%)",
"91 (77%)", "29 (40%)", "44 (60%)", "19 (33%)", "39 (67%)", "14 (27%)",
"38 (73%)", "15 (32%)", "31 (66%)", "21 (45.7%)", "25 (54.3%)",
"16 (37%)", "27 (63%)", "15 (41%)", "22 (59%)", "8 (24%)", "25 (76%)",
"4 (13%)", "26 (87%)"), total1 = c("69\n(23%)", "235\n(77%)",
"27\n(23%)", "91\n(77%)", "29\n(40%)", "44\n(60%)", "19\n(33%)",
"39\n(67%)", "14\n(27%)", "38\n(73%)", "15\n(32%)", "31\n(66%)",
"21\n(45.7%)", "25\n(54.3%)", "16\n(37%)", "27\n(63%)", "15\n(41%)",
"22\n(59%)", "8\n(24%)", "25\n(76%)", "4\n(13%)", "26\n(87%)"
)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", "data.frame"
))
And I plot it as follows:
ggplot(Result_counts, aes(fill=Reportable, y=count, x=as.factor(Result_Description), label = total)) +
geom_bar(position="stack", stat="identity")+
aes(stringr::str_wrap(as.factor(Result_Description), 40), count) +
labs(x = "", y = "Injury Count", fill = "")+
geom_text(size = 2, position = position_stack(vjust = 0.5)) +
ggtitle("Injury Plot")+
scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
theme_hc() +
coord_flip()
Cool right?
I have been messing around with plotly
and more importantly ggplotly
so remove my in-bar labels and instead have the labels appear on hover.
So how can I make this change to my ggplot code.
Once this is complete I still want to have a total on the right of each bar as a label which indicates sum of counts and percent of each full bar, irrespective of Reportable
.
So basically two goals
- Change existing labels to hovers
- Add a new label for the actual total count and percent of whole for each bar.