我生成了一个plotly
瀑布图,如下所示:
library(plotly)
library(dplyr)
data <- tibble(x = c("Sales", "Consulting", "Net revenue", "New Intermediate",
"Purchases", "Other expenses", "Profit before tax"),
measure = c("relative", "relative", "total", "absolute", "relative",
"relative", "total"),
text = c("+60", "+80", "", "", "-40",
"-20", "Total"),
y = c(60, 80, 0, 120, -40, -20, 0)) %>%
mutate(x = factor(x, x))
(fig <- data %>%
plot_ly() %>%
add_text(x = factor("Sales", levels = data$x),
y = 80, text = "Some Text") %>%
add_trace(type = "waterfall", measure = ~measure,
x = ~x, textposition = "outside", y= ~y, text =~text,
connector = list(line = list(color= "rgb(63, 63, 63)"))) %>%
layout(title = "Profit and loss statement 2018",
xaxis = list(title = ""),
yaxis = list(title = ""),
autosize = TRUE,
showlegend = FALSE))
由于存在第二个text
跟踪,默认情况下跟踪名称将添加到悬停文本中:
我知道我可以通过提供一个自定义的hovertemplate
来删除它,我在其中添加了一个空的<extra></extra>
标记。我的问题是我很喜欢默认的hovertemplate,我不知道如何复制它。
因此,我的问题是,我必须提供哪个hovertemplate
字符串才能像默认情况下一样获得确切的悬停,但不需要跟踪名称框?
这是我尝试过的,但它没有给予我的漂亮三角形,并显示了太多的文本为非relative
酒吧?
data %>%
plot_ly() %>%
add_trace(type = "waterfall", measure = ~measure,
x = ~x, textposition = "outside", y= ~y, text =~text,
connector = list(line = list(color= "rgb(63, 63, 63)")),
hovertemplate = "(%{x},%{y})<br>%{text}<br>%{delta}<br>Initial: %{initial}<extra></extra>") %>%
add_text(x = factor("Sales", levels = data$x),
y = 80, text = "Some Text") %>%
layout(title = "Profit and loss statement 2018",
xaxis = list(title = ""),
yaxis = list(title = ""),
autosize = TRUE,
showlegend = TRUE)
1条答案
按热度按时间v2g6jxz61#
由于您不需要图例,您可以在
add_trace(type = "waterfall", ......)
中添加name = ""
。