-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_ggiraph.Rmd
144 lines (109 loc) · 3.13 KB
/
widget_ggiraph.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
title: "ggiraph, plotly"
author: "State of the R"
date: "24-28/08/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
## Un premier exemple: nuages de points
```{r, message=FALSE}
library(palmerpenguins)
library(tidyverse)
library(ggiraph)
library(cowplot)
theme_set(theme_minimal())
```
On va travailler avec le jeu de données des pingouins de Palmer.
```{r}
penguins
ggplot(penguins) +
aes(x = bill_length_mm, y = bill_depth_mm, color = species) +
geom_point() +
scale_color_viridis_d()
```
Nous allons animer le graphe précédent pour que lorsqu'on passe la souris sur le point, un texte indiquant l'île s'affiche et tous les pinguoins de la même île se mettent à briller.
```{r}
p <-
ggplot(penguins) +
aes(x = bill_length_mm, y = bill_depth_mm, color = species) +
geom_point_interactive(aes(tooltip = str_c("Island: ", island), data_id = island)) +
scale_color_viridis_d()
girafe(ggobj = p)
```
Nous allons dessiner deux nuages de points (donc quatre axes plus la couleur), et lorsqu'on passe sur un point dans un des deux nuages, sa représentation dans l'autre graphe se mette à briller.
```{r}
penguins <- rowid_to_column(penguins)
p1 <-
ggplot(penguins) +
aes(x = bill_length_mm, y = bill_depth_mm, color = species) +
geom_point_interactive(aes(tooltip = str_c("Island: ", island), data_id = rowid)) +
scale_color_viridis_d() +
theme(legend.position = "none")
p2 <-
ggplot(penguins) +
aes(x = flipper_length_mm, y = body_mass_g , color = species) +
geom_point_interactive(aes(tooltip = str_c("Island: ", island), data_id = rowid)) +
scale_color_viridis_d() +
theme(legend.position = "none")
girafe(ggobj = plot_grid(p1, p2))
```
## Un deuxième exemple: "volcano plot"
Our goal is to add some interactivity to a "volcano plot":
- clicking on a gene opens the GeneCards page
- hovering on a gene shows other genes from the same pathway
- ...
### Data
Microarray data from a differential gene expression study (Chiaretti et al 2005)
```{r load-data}
diff_exp <- readRDS("data/diff_exp.rds")
head(diff_exp)
```
### Using plotly
Setup:
```{r load-packages, results="hide"}
library("plotly")
library("htmlwidgets")
```
```{r}
p <- plot_ly(data = diff_exp, x = ~meanDiff, y = ~logp) %>%
add_markers(
text = diff_exp[["geneName"]],
customdata = paste0("https://www.genecards.org/cgi-bin/carddisp.pl?gene=", diff_exp[["geneName"]]))
onRender(
p, "
function(el) {
el.on('plotly_click', function(d) {
var url = d.points[0].customdata;
window.open(url);
});
}
")
```
### Using ggiraph
```{r}
library("ggplot2")
library("ggiraph")
```
```{r}
diff_exp$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.genecards.org/cgi-bin/carddisp.pl?gene=", diff_exp[["geneName"]])
```
```{r}
p <- ggplot(diff_exp,
aes(meanDiff, logp)) +
geom_point()
p
```
```{r}
ip <- p + geom_point_interactive(
aes(data_id = geneName,
tooltip = geneName,
onclick = onclick))
girafe(ggobj = ip)
```
## Session information
```{r session-info}
sessionInfo()
```