See classification() for an explication on how the classification is done.

add_classification(
  p,
  x = NULL,
  y = NULL,
  ...,
  data = NULL,
  inherit = TRUE,
  sd,
  lcl = NULL,
  ucl = NULL,
  threshold,
  reference = 0,
  prob = 0.9,
  size = 20,
  link = c("identity", "log", "logit"),
  detailed = TRUE,
  signed = TRUE,
  labels = class_labels(lang = "en", detailed = detailed, signed = signed),
  text = NULL,
  hoverinfo = "text",
  ref_label = "reference",
  ref_colour = "#C04384"
)

Arguments

p

a plotly object

x

the x variable.

y

the y variable.

...

Arguments (i.e., attributes) passed along to the trace type. See schema() for a list of acceptable attributes for a given trace type (by going to traces -> type -> attributes). Note that attributes provided at this level may override other arguments (e.g. plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))).

data

A data frame (optional) or crosstalk::SharedData object.

inherit

inherit attributes from plot_ly()?

sd

the variable of the standard error on the link scale.

lcl

A vector of lower confidence limits.

ucl

A vector of upper confidence limits.

threshold

A vector of either 1 or 2 thresholds. A single threshold will be transformed into reference + c(-abs(threshold), abs(threshold)).

reference

The null hypothesis. Defaults to 0.

prob

The coverage of the confidence interval when calculated from the mean y and standard error sd. Note that the function assumes a normal distribution at the link scale.

size

Size of the point symbol.

link

the link between the natural scale and the link scale. Defaults to "identity".

detailed

TRUE indicates a detailed classification(); FALSE a coarse_classification(). Defaults to TRUE.

signed

TRUE indicates a signed classification; FALSE a classification with remove_sign(). Defaults to TRUE.

labels

a vector of labels for the classification hover information. See class_labels() for inspiration.

text

textual labels.

hoverinfo

Which hover information to display. Defaults to "text". When no "text" variable is specified, the function displays a formatted confidence interval.

ref_label

The label for the reference point. Will be used for the points where is.na(sd) or both is.na(lcl) and is.na(ucl).

ref_colour

The colour for the reference point.

See also

Other plotly add-ons: add_fan(), reference_shape(), reference_text()

Examples

# 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)
#> Loading required package: ggplot2
#> 
#> Attaching package: ‘plotly’
#> The following object is masked from ‘package:ggplot2’:
#> 
#>     last_plot
#> The following object is masked from ‘package:stats’:
#> 
#>     filter
#> The following object is masked from ‘package:graphics’:
#> 
#>     layout
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) )