Skip to contents

Thin wrapper around plotly::ggplotly() that fixes the rough edges you get when converting an epicurve plot:

Usage

epicurve_ggplotly(
  p,
  tooltip = "text",
  keep_symbol_legend = FALSE,
  width = NULL,
  height = NULL,
  ...
)

Arguments

p

A ggplot built with geom_epicurve().

tooltip

Passed to plotly::ggplotly(); defaults to "text" which uses the auto-generated tooltip.

keep_symbol_legend

If FALSE (the default), replace plotly's auto-generated legend for symbol layers with a custom annotation legend that shows the actual symbols (the auto legend would render keys as the placeholder "Aa" glyph). Set to TRUE to keep plotly's default behaviour.

width, height

Optional widget dimensions in pixels. When height is NULL (the default) the wrapper picks a height that matches the static-plot aspect for datetime axes; for date / numeric axes the htmlwidgets default is left alone.

...

Further arguments passed to plotly::ggplotly().

Value

A plotly htmlwidget.

Details

  • Uses the text aesthetic (auto-generated by geom_epicurve()) as the sole tooltip source.

  • Sets a per-trace hovertemplate so the default "trace 0", "trace 1", ... suffix never appears, and silences hover entirely for traces that carry no useful tooltip (the geom_segment used by annotate_event(), for example).

  • Re-injects the ggplot subtitle into the plotly title (plotly silently drops subtitle).

  • Swaps the auto-added coord_epicurve() for coord_cartesian() before conversion (plotly does not honour custom coords) and instead applies the equivalent aspect by setting the widget height, so hourly / sub-daily plots get the same shorter panel they get in static ggplot2 output.

  • For geom_epicurve() symbol mode, plotly cannot render the custom draw_key so the default legend swatch is the unhelpful "Aa" glyph. The wrapper hides plotly's auto-legend and replaces it with a small annotation block in the top-right of the panel that lists each symbol category mapping so the symbol-to-category meaning is preserved.

Examples

if (FALSE) { # \dontrun{
library(ggplot2)
cases <- simulate_outbreak(n = 30, prop_missing = 0)
p <- ggplot(cases, aes(x = onset_date)) +
  geom_epicurve(fill = "steelblue") +
  scale_y_epicurve() +
  theme_minimal()
epicurve_ggplotly(p)
} # }