I'm using a named vector to rename my facet labels, e.g.
facet_rename = c("old_name1" = "new_name1", "old_name2" = "new_name\n2")
I also want to wrap the text in the facet label. Ideally this would be at break points specified in the label by '\n', as in name2 above. However, I don't mind if it's done by characters. I can't seem to get it wrap any way though:
ggplot() +
facet_grid(rows = vars(facet), labeller = labeller(facet = facet_rename)) +
geom_line(data = my_plot_data, aes(x = rpt, y = height_rel, colour = group))
Any ideas?
I've tried label_wrap_gen(), as per the link below, but it doesn't seem to work with a renaming vector how to wrap text in ggplot for facet_grid labels
The label_wrap_gen() function doesn't directly work with named vectors for renaming facet labels in facet_grid. However, you can achieve text wrapping using the str_wrap() function from the stringr package within the labeller() function. Here's an example:
This code snippet uses the str_wrap() function inside the labeller() function to wrap the facet labels based on the specified width (in this case, 10 characters). Adjust the width parameter to suit your desired text wrapping length.
Make sure you have the stringr package installed and loaded in your R environment to use the str_wrap() function.