plotlyreference text Returns a list text you can pass to the annotations argument of plotly::layout()R/reference_text.R
reference_text.RdCreate plotlyreference text
Returns a list text you can pass to the annotations argument of
plotly::layout()
reference_text(
threshold,
reference = 0,
offset,
text = c("reference", "important decrease", "important increase")
)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.
An numeric vector with the offset between text and the lines.
In units of the y variable.
Defaults to 10% of the difference between reference and threshold.
A character vector with three elements with the text to display
on the reference line, bottom threshold line and upper threshold line.
Defaults to c("reference", "important decrease", "important increase").
Other plotly add-ons:
add_classification(),
add_fan(),
reference_shape()
# 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)
)