13.17 瀑布图
盈亏图
library(plotly)
library(dplyr)
dat <- data.frame(
x = c(
"销售", "咨询", "净收入",
"购买", "其他费用", "税前利润"
),
y = c(60, 80, 10, -40, -20, 0),
measure = c(
"relative", "relative", "relative",
"relative", "relative", "total"
)
) %>%
mutate(text = case_when(
y > 0 ~ paste0("+", y),
y == 0 ~ "",
y < 0 ~ as.character(y)
)) %>%
mutate(x = factor(x, levels = c(
"销售", "咨询", "净收入",
"购买", "其他费用", "税前利润"
)))
n_rows <- nrow(dat)
dat[nrow(dat), "text"] <- "累计"
# measure 取值为 'relative'/'total'/'absolute'
plotly::plot_ly(dat,
x = ~x, y = ~y, measure = ~measure, type = "waterfall",
text = ~text, textposition = "outside",
name = "收支", hoverinfo = "final",
connector = list(line = list(color = "gray")),
increasing = list(marker = list(color = "#66C2A5")),
decreasing = list(marker = list(color = "#FC8D62")),
totals = list(marker = list(color = "#8DA0CB"))
) %>%
plotly::layout(
title = "2018 年收支状态",
xaxis = list(title = "业务"),
yaxis = list(title = "金额"),
showlegend = FALSE
)图 12.65: 瀑布图