Using the waterfalls package. If I try to wrap long axis labels, the labels do wrap, but then they are arranged alphabetically, independently of the data.
Data:
structure(list(Factors = c("Start", "Differential A", "Move",
"EQ", "Differential (Premium)", "Transportation"), Cost = c(119.19,
-25.4, -0.832, -5.7, 4.93, -7.37)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
Without wrapping:
library(waterfalls)
waterfall(wf, calc_total=TRUE, rect_width=.4, fill_by_sign=TRUE,
total_rect_color="grey", rect_border=NA) +
labs(y="Cost", title ="Waterfall", x=NULL)
With str_wrap:
waterfall(wf, calc_total=TRUE, rect_width=.4, fill_by_sign=TRUE,
total_rect_color="grey", rect_border=NA) +
labs(y="Cost", title ="Waterfall", x=NULL) +
scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
Scale for x is already present. Adding another scale for x, which will replace the existing scale.
I've also tried wrap_format with the same result.
waterfall(wf, calc_total=TRUE, rect_width=.4, fill_by_sign=TRUE,
total_rect_color="grey", rect_border=NA) +
labs(y="Cost", title ="Waterfall", x=NULL) +
scale_x_discrete(labels = wrap_format(10))
How do I fix this?
Wrap the labels before your waterfall
function:
library(waterfalls)
library(tidyverse)
wf <- data.frame(factors = c('Start',
'Differential A',
'Move',
'EQ',
'Differential (Premium)',
'Transportation'),
cost = c(119.19, -25.4, -0.832, -5.7, 4.93, -7.37))
wf %>%
mutate(factors = str_wrap(factors, width = 10)) %>%
waterfall(calc_total=TRUE, rect_width=.4, fill_by_sign=TRUE,
total_rect_color="grey", rect_border=NA) +
labs(y="Cost", title ="Waterfall", x=NULL)