Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to wrap text in ggplot for facet_grid labels

I have been searching how to wrap text. It seems there should be a way to use labeller = label_wrap_gen(3) but I keep getting an error: --- Error in margins(vars, margins) : unused argument (margins)

Here is part of my code:

#simpson by protected status for domain FKNMS
ggplot(data = fk_strata_abun_diversity, aes(x = YEAR)) +
geom_point(aes(y = strata_simpson, color = "strata_simpson"),color = "blue") +
geom_line(aes(y = strata_simpson, color = "strata_simpson"), color = "blue") +
facet_grid(STRAT ~ protected_status, 
         labeller = labeller(.rows = strata_names, .cols = protected_status_names),
         label_wrap_gen(width = 2)) + #error: in margins(vars, margins) : unused argument (margins) ?? 
labs(x = "Year", y = "Effective Number of Species") +
ggtitle("Simpson Diveristy of Reef Fish in the Florida Keys by Strata") + 
theme(plot.title = element_text(hjust = 0.5, face = 'bold', size = 12)) +
scale_x_continuous(limits = c(1999, 2016), breaks = c(1999:2016)) +
scale_y_continuous(limits= c(0, 25), breaks = c(5,10,15,20,25))

Thank you in advance for the help

like image 426
Megan Elizabeth Avatar asked May 05 '17 04:05

Megan Elizabeth


2 Answers

I found labeller = labeller(label_wrap_gen(width = 2... does not wrap.

Try

facet_grid(STRAT ~ protected_status,
     labeller = label_wrap_gen(width = 2, multi_line = TRUE))
like image 107
Kbushu Avatar answered Nov 29 '22 06:11

Kbushu


the following will work for facet_grid() and facet_wrap():

facet_grid(labeller = labeller(facet_category = label_wrap_gen(width = 16)))

where facet_category is the faceting variable to modify and width sets the maximum number of characters before wrapping.

multi_line is only needed if you have specified multiple factors in your faceting formula (eg. ~first + second)

like image 30
itsmisterbrown Avatar answered Nov 29 '22 07:11

itsmisterbrown