-
Notifications
You must be signed in to change notification settings - Fork 4
/
03-interactive-graphics.Rmd
53 lines (42 loc) · 2.7 KB
/
03-interactive-graphics.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
---
title: "Interactive graphics"
author: "John Franchak"
output: html_document
---
```{r setup, include = FALSE}
library(tidyverse)
library(here)
ds <- read_csv(here("data_cleaned","cleaned.csv"))
ds <- ds %>% mutate(across(all_of(c("stim", "age_group", "watched")), as_factor))
ds$age <- ds$age / 365.25
theme_update(text = element_text(size = 16),
axis.text.x = element_text(size = 16, color = "black"), axis.title.x = element_text(size = 21),
axis.text.y = element_text(size = 16, color = "black"), axis.title.y = element_text(size = 21),
panel.background = element_blank(),panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "white"),
axis.ticks.length = unit(.25, "cm"), axis.ticks = element_line(size = 1, lineend = "round"),
legend.key = element_rect(fill = "white"))
age_groups <- unique(ds$age_group)
custom_palette <- c("#999999", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2", "#D55E00") %>% set_names(age_groups)
```
### Plotly
Sometimes it's nice (especially for EDA), to have plots that are scrollable and zoomable. The `plotly` package allows you to do this. Plotly is its own thing <https://plotly.com/r/> and is meant for making customized dashboards and interactive web graphics. There's much, much more it can do. It also works in Python. We don't really need to learn how plotly works to get a basic plot, because the `ggplotly` command will can convert a ggplot into a plotly plot.
```{r, message = FALSE}
library(plotly)
p1 <- ds %>% ggplot(aes(x = age, y = AUC_dist, color = age_group)) +
geom_point() +
theme(legend.position = "none") +
scale_color_manual(values = custom_palette)
ggplotly(p1)
```
### Animated plots
Another way to extend ggplot is using the `gganimate` package. By default, the animations render as .gif which makes them embeddable in an html presentation and shareable online. You can get fancy and render them as video files to put in presentations. I'm scratching the surface here, but the easiest thing to do is map a discrete factor to `transition_states` and you will get an animation that is split by that factor. It's pretty much like `facet_wrap`, but uses time instead of space. Putting `"{closest_state}"` into the title will label the level of the factor in the animation so that you know what you're looking at.
```{r, cache = TRUE, message = FALSE, warning= FALSE}
library(gganimate)
library(gifski)
ds %>% ggplot(aes(x = age, y = AUC_dist, color = age_group, group = id)) +
geom_point() +
transition_states(stim) +
ggtitle("{closest_state}") +
scale_color_manual(values = custom_palette)
```