Overview
The {plotly} library provides a
ggplotly
function works to convert ggplot objects to
interactive html widgets.
The {simplevis} package provides:
- a
text_var
argument withingg_*
wrapper functions for use as the tooltip - a
mutate_text
function to create a tooltip easily based on all or a subset of variables in the data - a
plotly_camera
function to turn off all widgets other than the camera -
plotly_col_legend
function to manipulate the order of plotly legends.
Turning a ggplot into an html widget
The plotly::ggplotly
function provides the ability to
convert the ggplot to an interactive object.
plot <- gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species)
plotly::ggplotly(plot)
Turning off widgets other than the camera
The plotly_camera
function turns off all widgets other
than the camera to make a cleaner graph.
plot <- gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species)
plotly::ggplotly(plot) %>%
plotly_camera()
Creating nice tooltips
For bar/hbar, line, point, pointrange/hpointrange, smooth, tile and sf functions, {simplevis} provides a method to customise toolips.
A variable can be added to the text_var
in the
{simplevis} function. This variable is then used in the ggplotly tooltip
when tooltip = text
is added to the ggplotly
function.
{simplevis} provides a mutate_text
function which can
produce a variable that is a string or variable names and values for a
tooltip. Note this function converts column names to sentence case using
the snakecase::to_sentence_case
function.
plot_data <- penguins %>%
mutate_text()
plot <- gg_point_col(plot_data,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species,
text_var = text)
plotly::ggplotly(plot, tooltip = "text") %>%
plotly_camera()
The mutate_text
function uses all variables in the
dataset by defalut, but a subset can be used if desired.
plot_data <- penguins %>%
mutate_text(c("species", "flipper_length_mm"))
plot <- gg_point_col(plot_data,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species,
text_var = text)
plotly::ggplotly(plot, tooltip = "text") %>%
plotly_camera()
Controlling the plotly colour legend order
Sometimes the ggplotly legend displays elements in a different order
to how you want them. You can reverse them or specify a specific order
using plotly_col_legend
. Note, this only affects the legend
elements.
plot_data <- dplyr::sample_frac(ggplot2::diamonds, 0.05)
plot <- gg_point_col(plot_data,
x_var = carat,
y_var = price,
col_var = color)
plotly::ggplotly(plot) %>%
plotly_camera() %>%
plotly_col_legend(order = c(2:7, 1))
sf
maps
sf ggplots can also be converted to interactive ggplotly objects using the same method.
plot_data <- example_point %>%
mutate_text()
plot <- gg_sf_col(plot_data,
col_var = trend_category,
text_var = text,
borders = example_borders)
plotly::ggplotly(plot, tooltip = "text") %>%
plotly_camera()
col_cuts <- as.vector(quantile(example_point$median, probs = c(0, 0.25, 0.5, 0.75, 1)))
plot_data <- example_point %>%
mutate(quartile = case_when(median < col_cuts[2] ~ "Q1",
median < col_cuts[3] ~ "Q2",
median < col_cuts[4] ~ "Q3",
TRUE ~ "Q4")) %>%
mutate_text()
plot <- gg_sf_col_facet(plot_data,
col_var = trend_category,
facet_var = quartile,
text_var = text,
borders = example_borders)
plotly::ggplotly(plot, tooltip = "text") %>%
plotly_camera()