Interactive epicurve via plotly with sensible defaults
Source:R/epicurve_ggplotly.R
epicurve_ggplotly.RdThin 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 toTRUEto keep plotly's default behaviour.- width, height
Optional widget dimensions in pixels. When
heightisNULL(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().
Details
Uses the
textaesthetic (auto-generated bygeom_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 byannotate_event(), for example).Re-injects the ggplot
subtitleinto the plotly title (plotly silently dropssubtitle).Swaps the auto-added
coord_epicurve()forcoord_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 customdraw_keyso 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 eachsymbol categorymapping 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)
} # }