提问人:zxcvfi 提问时间:10/26/2023 最后编辑:zxcvfi 更新时间:10/26/2023 访问量:39
瀑布图(瀑布图包) ggplot - 包装长 x 轴标签
Waterfall chart (waterfalls package) ggplot - wrapping long x-axis labels
问:
使用 waterfalls 包。 如果我尝试换行长轴标签,标签确实会换行,但它们是按字母顺序排列的,与数据无关。
数据:
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))
不换行:
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)
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))
x 的刻度已存在。 为 x 添加另一个刻度,这将替换现有刻度。
我也尝试过wrap_format结果相同。
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))
如何解决此问题?
答:
2赞
Seth
10/26/2023
#1
将标签包装在函数之前:waterfall
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)
评论
0赞
zxcvfi
10/26/2023
这很有帮助。谢谢。
评论