J'ai donc un cadre de données comme suit
df <- structure(list(Reportable = c("A", "B",
"A", "B", "A",
"B", "A", "B",
"A", "B", "A",
"B", "A", "B"
), Location1_Description = c("MAIN/BRANCH", "MAIN/BRANCH", "YARD",
"YARD", "PART", "PART", "SHOP",
"SHOP", "LOT", "LOT", "HIGHWAY/ROADWAY",
"HIGHWAY/ROADWAY", "OFFICE", "OFFICE"
), count = c(146L, 447L, 83L, 241L, 44L, 89L, 38L, 83L, 16L,
28L, 4L, 30L, 11L, 21L), pct = c("25%", "75%", "26%", "74%",
"33%", "67%", "31%", "69%", "36%", "64%", "12%", "88%", "33%",
"64%"), total = c("146 (25%)", "447 (75%)", "83 (26%)", "241 (74%)",
"44 (33%)", "89 (67%)", "38 (31%)", "83 (69%)", "16 (36%)", "28 (64%)",
"4 (12%)", "30 (88%)", "11 (33%)", "21 (64%)"), total1 = c("146\n(25%)",
"447\n(75%)", "83\n(26%)", "241\n(74%)", "44\n(33%)", "89\n(67%)",
"38\n(31%)", "83\n(69%)", "16\n(36%)", "28\n(64%)", "4\n(12%)",
"30\n(88%)", "11\n(33%)", "21\n(64%)")), row.names = c(NA, -14L
), class = c("tbl_df", "tbl", "data.frame"))
Et je trace comme tel
library(tidyverse)
library(stringr)
library(ggthemes)
ggplot(df, aes(fill=Reportable, y=count, x=as.factor(Location1_Description), label = total)) +
geom_bar(position="stack", stat="identity")+
aes(stringr::str_wrap(as.factor(Location1_Description), 15), count) +
labs(x = "", y = "Injury Count", fill = "")+
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
theme_hc() +
coord_flip()
Si vous remarquez, les étiquettes se chevauchent dans le graphique. J'ai essayé de changer la taille des étiquettes mais je n'arrive pas à les rendre lisibles. Comment puis-je faire en sorte que le contenu de chaque mini-barre soit clair ?
Le résultat final devrait être un graphique dans lequel je peux voir clairement quel nombre/pct ( total
) est pour chaque barre.