0

I need help in wrapping the labels of legend in ggplot. I tried various option including

  • str_wrap(model, width=20)
  • scale_colour_discrete(labels = function(x) str_wrap(x, width = 5))
  • guides(colour = guide_legend(nrow = 2))

but without any success

library(ggplot2)

ggplot(coefs, aes(x = estimate, y = term, colour = model)) +
  geom_vline(xintercept = 1, lty = 1, color = "yellow", size = 1) +
  geom_pointrange(aes(xmin = conf.low, xmax = conf.high),
    position = position_dodge(width = 0.5)
  ) +
  facet_wrap(~type, scale = "free") +
  geom_text(aes(x = estimate, label = sprintf("%0.2f", estimate)), position = position_dodge(0.5), vjust = -0.5) +
  labs(x = "gy", y = "age") +
  scale_color_manual(
    name = "Model",
    labels = c("Fullfasdfasdfad-asdkljaflsdjfals;jfasdf", "Subadfasdfaasdfasdfasdfsdfasdfasf"),
    values = c("dodgerblue4", "firebrick4")
  )

enter image description here

Data

coefs <- structure(list(model = c(
  "all_adj", "all_adj", "all_adj", "all_adj",
  "adj_sub", "adj_sub"
), term = c(
  "ageb", "agec", "ageb", "agec",
  "ageb", "ageb"
), type = c(
  "count", "count", "zero", "zero", "count",
  "zero"
), estimate = c(
  0.937781183281121, 1.09774595782208, 0.895560088459192,
  0.891707940838411, 0.76445315191301, 1.01406754426526
), conf.low = c(
  0.422176961883128,
  0.319479297647212, 0.273199977915238, 0.132809852827134, 0.175087960312586,
  0.186498610242251
), conf.high = c(
  2.08309222699744, 3.77190696483063,
  2.93568058885374, 5.98707878088866, 3.33768592898348, 5.51389087026671
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
camille
  • 16,432
  • 18
  • 38
  • 60
skpak
  • 79
  • 6
  • As your labels don't contain and whitespace "automatic" wrapping via `str_wrap` will not work. Why not adding a simple linebreak "\n"? – stefan Jan 26 '23 at 18:34
  • @stefan, thanks for your reply. in my original data there is space in the labeling but still str_wrap did not work. – skpak Jan 26 '23 at 18:52
  • 1
    Hm. In that case I would be helpful to add your "real" labels to your reprex. Also, you could simplify your example code considerably by just providing your final `coefs` dataframe used for the plot. Doing so you could remove all the code to create your models and one does not require all the packages for this step. – stefan Jan 26 '23 at 19:32
  • FYI: I just stripped down your code to a minimal example – stefan Jan 26 '23 at 20:28
  • Do you mean "warping," which you wrote, or "wrapping"? It's unclear what warped text would mean – camille Jan 26 '23 at 20:28
  • Hello, @camille Thank you for catching my typo. the wrapping. since the legend's label is very long and squashes the actual graph. Consequently, I intended to divide the legend label into two three lines. – skpak Jan 27 '23 at 15:48
  • So are the labels in @stefan's edit representative of what you're working with? Or do you still need to add a [mcve] that mirrors the whitespacing you do/don't have in your data? – camille Jan 27 '23 at 16:57

0 Answers0