Reputation: 1105
My data is as follows:
df <- structure(list(Reportable = c("Non-Reportable Injury", "Reportable Injury",
"Non-Reportable Injury", "Reportable Injury", "Non-Reportable Injury",
"Reportable Injury", NA, "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"), Event_Description = c(NA, NA, "OVEREXERTION",
"OVEREXERTION", "SLIPPED, FELL, STUMBLED, OTHER", "SLIPPED, FELL, STUMBLED, OTHER",
"SLIPPED, FELL, STUMBLED, OTHER", "STRUCK BY OBJECT", "STRUCK BY OBJECT",
"STRUCK AGAINST OBJECT", "STRUCK AGAINST OBJECT", "SLIPPED, FELL, STUMBLED, ETC. DUE TO CLIMATIC CONDITION (RAIN, SNOW, ICE, E",
"SLIPPED, FELL, STUMBLED, ETC. DUE TO CLIMATIC CONDITION (RAIN, SNOW, ICE, E",
"CAUGHT, CRUSHED, PINCHED, OTHER.", "CAUGHT, CRUSHED, PINCHED, OTHER.",
"SLIPPED, FELL, STUMBLED, ETC. DUE TO OBJECT, E.G.,BALLAST, SPIKE, MATERIAL,",
"SLIPPED, FELL, STUMBLED, ETC. DUE TO OBJECT, E.G.,BALLAST, SPIKE, MATERIAL,",
"ASSAULTED BY OTHER", "ASSAULTED BY OTHER", "LOST BALANCE", "LOST BALANCE",
"STRUCK BY FALLING OBJECT", "STRUCK BY FALLING OBJECT", "SLIPPED, FELL, STUMBLED, ETC. DUE TO IRREGULAR SURFACE, E.G., DEPRESSION, S",
"SLIPPED, FELL, STUMBLED, ETC. DUE TO IRREGULAR SURFACE, E.G., DEPRESSION, S"
), count = c(238L, 33L, 62L, 202L, 33L, 108L, 1L, 36L, 85L, 37L,
48L, 22L, 55L, 21L, 52L, 14L, 41L, 9L, 34L, 7L, 32L, 13L, 25L,
8L, 27L), pct = c("88%", "12%", "23%", "77%", "23%", "76%", "1%",
"30%", "70%", "44%", "56%", "29%", "71%", "29%", "71%", "25%",
"75%", "21%", "79%", "18%", "82%", "34%", "66%", "23%", "77%"
), total = c("238 (88%)", "33 (12%)", "62 (23%)", "202 (77%)",
"33 (23%)", "108 (76%)", "1 (1%)", "36 (30%)", "85 (70%)", "37 (44%)",
"48 (56%)", "22 (29%)", "55 (71%)", "21 (29%)", "52 (71%)", "14 (25%)",
"41 (75%)", "9 (21%)", "34 (79%)", "7 (18%)", "32 (82%)", "13 (34%)",
"25 (66%)", "8 (23%)", "27 (77%)")), row.names = c(NA, -25L), class = c("tbl_df",
"tbl", "data.frame"))
So it's a lot of verbage and as a result in RMarkdown all the percentages are overlapping each other as are the labels of the bars. How would I avoid this?
library(ggplot2)
library(ggthemes)
library(stringr)
ggplot(event_counts, aes(fill=Reportable, y=count, x=as.factor(Event_Description), label = total)) +
geom_bar(position="dodge", stat="identity")+
aes(stringr::str_wrap(as.factor(Event_Description), 15), count) +
labs(x = "", y = "Injury Count", fill = "")+
geom_text(position = position_dodge(width = .9), #move to center of bars
vjust = -0.5, #nudge above top of bar
size = 3) +
ggtitle("Title")+
scale_fill_manual(values = c("darkorange", "cornflowerblue") ) +
theme_hc() +
theme(axis.text.x=element_text(angle = 90, vjust = 0.5))
As you can see from the plot above, the words are running on each other and in many places we see there are percentages running over eachother.
Because this is done in Markdown, I can't simply drag it bigger. :(
Upvotes: 1
Views: 1080
Reputation: 173803
There's no "magic bullet" here due to the number of points you are plotting, the fixed plot size, and the long x axis category text. You have to compromise somewhere.
The percentage labels above the bars are relatively easy to fix by swapping the space for a line break, but the x axis labels are too long even for the new guide_axis
function to give you a nice result. I think on balance you just need to lengthen your stringr::wrap
to allow all the labels to fill a maximum of three lines. That way you avoid clashes:
library(ggplot2)
library(ggthemes)
library(stringr)
event_counts %>%
mutate(total = gsub(" \\(", "\n\\(", total)) %>%
ggplot(aes(fill = Reportable, y = count, x = factor(Event_Description), label = total)) +
geom_col(position = "dodge") +
aes(str_wrap(as.factor(Event_Description), 30), count) +
labs(x = "", y = "Injury Count", fill = "") +
lims(y = c(0, 260)) +
geom_text(position = position_dodge(width = .9), vjust = -0.5, size = 3) +
ggtitle("Title") +
scale_fill_manual(values = c("darkorange", "cornflowerblue")) +
scale_x_discrete(guide = guide_axis(angle = 90))
Upvotes: 1