/
v04_Using_plotly_with_gglaplot.Rmd
121 lines (92 loc) · 5.09 KB
/
v04_Using_plotly_with_gglaplot.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: "Using plotly with gglaplot"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using plotly with gglaplot}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "",
dpi = 300
)
```
The R package [plotly](https://plotly.com/r/), 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:
* It can mostly only be used on in built ggplot2 geoms (the only custom gglaplot geom it works for is `ggla_line`, although more may be added)
* Not all features of a ggplot can be converted (e.g. subtitles and captions are lost in the conversion)
For more complex figures it could be easier to start with base plotly, details on how to do this can be found [here](https://plotly-r.com/) and [here](https://plotly.com/r/).
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(gglaplot)
library(plotly)
library(sf)
library(scales)
library(htmlwidgets)
```
Here's a simple ggplot line chart:
```{r}
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:
```{r}
ggplotly(plot, width = 800, height = 500) %>%
ggla_plotly_settings()
```
`ggla_plotly_settings` includes the same arguments as `theme_gla`, including using the inverse theme.
```{r}
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](https://plotly-r.com/) and [here](https://plotly.com/r/) 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.
```{r}
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
```{r}
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:
```{r, fig.cap="Contains Ordnance Survey data Crown copyright and database right [2015]", fig.align="left"}
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")
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(...))`](https://plotly.com/r/reference/#layout-annotations), 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:
```{r, eval=FALSE}
saveWidget(plotly_plot, file = "plotly_plot.html")
```