如何在ggplot中为facet_grid标签包装文本

时间:2017-05-05 04:14:42

标签: textwrapping facet-grid

我一直在搜索如何包装文字。似乎应该有一种方法来使用labeller = label_wrap_gen(3),但我一直收到错误: ---边距错误(变量,边距):未使用的参数(边距)

以下是我的代码的一部分:

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

提前感谢您的帮助

2 个答案:

答案 0 :(得分:2)

我发现labeller = labeller(label_wrap_gen(width = 2 ...没有换行。

尝试

facet_grid(STRAT ~ protected_status,
     labeller = label_wrap_gen(width = 2, multi_line = TRUE))

答案 1 :(得分:0)

以下内容适用于facet_grid()facet_wrap()

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

其中facet_category是要修改的构面变量,而width设置包装前最多个字符

multi_line仅在您在构面公式中指定了多个因素(例如~first + second)时才需要