vignettes/v04_Using_plotly_with_gglaplot.Rmd
v04_Using_plotly_with_gglaplot.Rmd
The R package plotly, allows you to make interactive plots in R. These plots can either be made using plotly itself, or simple plots can be converted from ggplot2 using ggplotly
. gglaplot includes functions which apply styling to the plots to be more in keeping with the GLA IU style guide.
The conversion of ggplots to plotlys using ggplotly
isn’t perfect and is still undergoing development. The main limitations are:
ggla_line
, although more may be added)For more complex figures it could be easier to start with base plotly, details on how to do this can be found here and here.
library(tidyverse) library(gglaplot) library(plotly) library(sf) library(scales) library(htmlwidgets)
Here’s a simple ggplot line chart:
pal <- gla_pal(gla_theme = "default", palette_type = "highlight", n = c(1, 1)) plot <- ggplot(data = LDNUK, mapping = aes(x = Year, y = GPG, group = location, colour = location)) + scale_y_continuous(labels = dollar_format(prefix = "", suffix = "%", accuracy = 0.1)) + labs(title = "Gender Pay Gap - Total (Median) - London VS UK") + scale_colour_manual(values = pal) + ggla_line() plot
Feeding this plot into ggplotly
will produce a basic plotly plot, including a default hovertool and basic interactivity and piping it into ggla_plotly_settings
will apply gglaplot styling:
ggplotly(plot, width = 800, height = 500) %>% ggla_plotly_settings() Warning: `group_by_()` is deprecated as of dplyr 0.7.0. Please use `group_by()` instead. See vignette('programming') for more help This warning is displayed once every 8 hours. Call `lifecycle::last_warnings()` to see where this warning was generated.
ggla_plotly_settings
includes the same arguments as theme_gla
, including using the inverse theme.
ggplotly(plot, width = 800, height = 500) %>% ggla_plotly_settings(gla_theme = "inverse")
ggla_plotly_settings
includes adjustments to the layout
, style
and config
settings of plotly plots. To make additional adjustments any layout arguments can be added directly to ggla_plotly_settings(...)
. For changes to the style
and config
of the plot, the plot can either be piped into style()
or config()
after ggla_plotly_settings
or the sub-functions included in ggla_plotly_settings()
(ggla_plotly_layout
, ggla_plotly_style
, ggla_plotly_config
) can be used instead with each of them accepting the arguments of their equivalent plotly functions. See here and here for more details.
To adjust what it shown in the hover tool add a text
aesthetic to the ggplot plot and reference it in tooltip
in gglaplotly.
plot <- ggplot(data = LDNUK, mapping = aes( x = Year, y = GPG, group = location, colour = location, text = comma(x = GPG, accuracy = 0.5, suffix = "%"))) + scale_colour_manual(values = pal) + ggla_line() + scale_y_continuous(labels = dollar_format(prefix = "", suffix = "%", accuracy = 0.1)) + labs(title = "Gender Pay Gap - Total (Median) - London VS UK") ggplotly(plot, tooltip = "text", width = 800, height = 500) %>% ggla_plotly_settings()
Range sliders can be added to view certain periods of data
ggplotly(plot, tooltip = "text", dynamicTicks = TRUE, width = 800, height = 500) %>% ggla_plotly_settings(xaxis = list(rangeslider = list(type = "date")))
gglaplotly
also includes some default settings to tidy up the rendering of maps:
pal <- gla_pal(gla_theme = "default", palette_type = "diverging", main_colours = c("blue", "red"), n = 6, remove_margin = "both") EURef <- EURef %>% st_as_sf() plot <- ggplot(data = EURef) + geom_sf(mapping = aes(geometry = geometry, fill = CatLeave, text = CatLeave), color = gla_default$background) + scale_fill_manual(values = pal) + labs(title = "Leave Vote in the 2016 EU Referendum") Warning: Ignoring unknown aesthetics: text plotly_plot <- ggplotly(plot, tooltip = "text", width = 800, height = 500) %>% ggla_plotly_settings() plotly_plot
Note: As captions cannot be carried over from ggplots, a figure caption has been added in RMarkdown (using fig.cap=
in the chunk header). It is also possible to add an annotation to a plotly figure using layout(annotation = list(...))
, but the positioning will depend on the other features of the figure.
Plotly figures can be saved as standalone html files using the htmlwidgets
package:
saveWidget(plotly_plot, file = "plotly_plot.html")