I want to wrap long category labels on my ggplot in R. I know I can use str_wrap() and scale_x_manual() to wrap them onto a new line after, say 10 words. However, several of my category labels are king strings of continuous characters, without spaces (eg they have underscores instead of spaces). How can I wrap onto new lines after, say 40 characters?
Here's an example
df = data.frame(x = c("label", "long label with spaces", "long_label_with_no_spaces_in_it_which_is_too_lon"),
y = c(10, 15, 20))
ggplot(df, aes(x, y)) +
geom_bar(stat = "identity") +
scale_x_discrete(labels = function(x) str_wrap(x, width = 10))