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")
)
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"))