plotly
references Returns a list shapes you can pass to the shapes
argument of plotly::layout()
R/reference_shape.R
reference_shape.Rd
Create plotly
references
Returns a list shapes you can pass to the shapes
argument of
plotly::layout()
reference_shape(
threshold,
reference = 0,
colour = "black",
line = FALSE,
horizontal = TRUE
)
A vector of either 1 or 2 thresholds.
A single threshold will be transformed into
reference + c(-abs(threshold), abs(threshold))
.
The null hypothesis. Defaults to 0.
The colour for the references.
Defaults to "black"
.
display the threshold
as a line (TRUE
) or a ribbon (FALSE
).
Defaults to FALSE
.
Display horizontal reference when TRUE
(default).
Display vertical reference when FALSE
.
Other plotly add-ons:
add_classification()
,
add_fan()
,
reference_text()
# All possible classes
z <- data.frame(
estimate = c(-0.5, 0, 0.5, 1.5, 1, 0.5, 0, -0.5, -1, -1.5),
sd = c(rep(0.8, 3), rep(0.3, 7))
)
z$lcl <- qnorm(0.05, z$estimate, z$sd)
z$ucl <- qnorm(0.95, z$estimate, z$sd)
classification(z$lcl, z$ucl, threshold = 1) -> z$effect
c(
"?" = "unknown\neffect", "?+" = "potential\npositive\neffect",
"?-" = "potential\nnegative\neffect", "~" = "no effect",
"+" = "positive\neffect", "-" = "negative\neffect",
"+~" = "moderate\npositive\neffect", "-~" = "moderate\nnegative\neffect",
"++" = "strong\npositive\neffect", "--" = "strong\nnegative\neffect"
)[as.character(z$effect)] -> z$x
z$x <- factor(z$x, z$x)
z$display <- paste(
"estimate:", format_ci(z$estimate, lcl = z$lcl, ucl = z$ucl)
)
# Simulated trend
set.seed(20190521)
base_year <- 2000
n_year <- 20
trend <- data.frame(
dt = seq_len(n_year),
change = rnorm(n_year, sd = 0.2),
sd = rnorm(n_year, mean = 0.1, sd = 0.01)
)
trend$index <- cumsum(trend$change)
trend$lcl <- qnorm(0.025, trend$index, trend$sd)
trend$ucl <- qnorm(0.975, trend$index, trend$sd)
trend$year <- base_year + trend$dt
trend$display <- paste(
"index:", format_ci(trend$index, lcl = trend$lcl, ucl = trend$ucl)
)
th <- 0.25
ref <- 0
library(plotly)
plot_ly(z, x = ~x, y = ~estimate) |>
add_fan(sd = ~sd, text = ~display) |>
add_classification(lcl = ~lcl, ucl = ~ucl, threshold = 1) |>
layout(
hovermode = "x unified",
shapes = reference_shape(threshold = 1),
annotations = reference_text(threshold = 1)
)
plot_ly(z, x = ~x, y = ~estimate) |>
add_fan(sd = ~sd, step = 0.1, text = ~display) |>
add_classification(
lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE
) |>
layout(
shapes = reference_shape(threshold = 1, line = TRUE),
annotations = reference_text(threshold = 1)
)
plot_ly(z, x = ~x, y = ~estimate) |>
add_fan(sd = ~sd, step = 0.2, hoverinfo = "none") |>
add_classification(
lcl = ~lcl, ucl = ~ucl, threshold = 1, signed = FALSE
) |>
layout(shapes = reference_shape(threshold = 1))
plot_ly(z, x = ~x, y = ~estimate) |>
add_fan(sd = ~sd, step = 0.3) |>
add_classification(
lcl = ~lcl, ucl = ~ucl, threshold = 1, detailed = FALSE, signed = FALSE,
text = ~display
) |>
layout(
shapes = reference_shape(threshold = 1, line = TRUE)
)
# trend
plot_ly(data = trend, x = ~year, y = ~index) |>
add_fan(sd = ~sd, text = ~display, hoverinfo = "text") |>
add_classification(sd = ~sd, threshold = th) |>
layout(
hovermode = "x unified", hoverdistance = 1,
shapes = reference_shape(threshold = th, reference = ref),
annotations = reference_text(threshold = th, reference = ref)
)