I have the following data:
df = structure(list(x = structure(1:9, .Label = c("A", "B",
"C", "D", "E", "F", "G", "H", "I"), class = "factor"),
measure = c("relative", "relative", "relative", "relative",
"relative", "relative", "relative", "relative", "total"),
text = c("9184", "0", "0", "3153", "-1364", "-979", "-140",
"0", "9508"), y = c(9184, 0, 0, 3153, -1364, -979, -140,
0, 9508)), class = "data.frame", row.names = c(NA, -9L))
and am making a waterfall, but want the values to start at a reasonable value, not 0, as there is a lot of empty space, and is not pleasing as shown below under A and I:
I would like it to start at like 6000 and tried to adjust my function by including base = 6000
, as per https://plotly.com/r/waterfall-charts/ but that did not work.
Does anyone else have any suggestions.
my function for creating the fall is as follows:
plot_value_waterfall = function(df, name, type){
rgb_total="rgb(0,0,255)"
bridge_relation = ifelse(type == "3m", "Bridge 3m", "Bridge LTM")
fig <- plot_ly(
df, name = "Company 1", type = "waterfall", measure = ~measure,
x = ~x, textposition = "outside", y= ~y, text =~text,
connector = list(line = list(color= "rgb(63, 63, 63)")))
fig <- fig %>%
layout(title = paste(name,bridge_relation),
xaxis = list(title = ""),
yaxis = list(title = ""),
autosize = TRUE,
showlegend = FALSE)
fig %>%
layout(xaxis = list(tickangle = 45),
shapes = list(
list(type = "rect",
fillcolor = rgb_total,
line = list(color = rgb_total),
opacity = 1,
x0 = -0.4, x1 = 0.4, xref = "x",
y0 = 0.0, y1 = df$y[1], yref = "y"),
list(type = "rect",
fillcolor = rgb_total,
line = list(color = rgb_total),
opacity = 1,
x0 = dim(df)[1] - 1 - 0.4, x1 = dim(df)[1] - 1 + .4, xref = "x",
y0 = 0.0, y1 = df$y[dim(df)[1]], yref = "y")
))
}
I'm not entirely sure that I've captured exactly what you have set out to do. I feel like what you wrote in your question says something entirely different than what you coded. If I understand correctly, you want to change the range on the y-axis....literally. (If I've misunderstood, let me know.)
From the code in your question, I extracted your plot and added a specific y-axis range.
library(plotly)
plot_ly(
df, name = "Company 1", type = "waterfall", measure = ~measure,
x = ~x, textposition = "outside", y= ~y, text =~text,
connector = list(line = list(color= "rgb(63, 63, 63)"))) %>%
layout( # title = paste(name,bridge_relation),
xaxis = list(title = ""),
yaxis = list(title = "", range = c(6000, 12900)), # <---- defined range!
autosize = TRUE,
showlegend = FALSE)