13.17 瀑布图
盈亏图
library(plotly)
library(dplyr)
<- data.frame(
dat x = c(
"销售", "咨询", "净收入",
"购买", "其他费用", "税前利润"
),y = c(60, 80, 10, -40, -20, 0),
measure = c(
"relative", "relative", "relative",
"relative", "relative", "total"
)%>%
) mutate(text = case_when(
> 0 ~ paste0("+", y),
y == 0 ~ "",
y < 0 ~ as.character(y)
y %>%
)) mutate(x = factor(x, levels = c(
"销售", "咨询", "净收入",
"购买", "其他费用", "税前利润"
)))
<- nrow(dat)
n_rows nrow(dat), "text"] <- "累计"
dat[
# measure 取值为 'relative'/'total'/'absolute'
::plot_ly(dat,
plotlyx = ~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"))
%>%
) ::layout(
plotlytitle = "2018 年收支状态",
xaxis = list(title = "业务"),
yaxis = list(title = "金额"),
showlegend = FALSE
)