ggiraph:创建交互式ggplot2图表

ggiraph:创建交互式ggplot2图表

本文是学习Make ‘ggplot’ Graphics Interactive的笔记,讲述了如何使用ggiraph包创建交互式ggplot2图表。

安装

R R
1
devtools::install_github('davidgohel/ggiraph')

一些用法

R
1
2
3
4
5
6
7
8
9
10
library(ggiraph)
library(ggplot2)

g <- ggplot(
mpg,
aes(x = displ, y = cty,
color = hwy)) +
hrbrthemes::theme_ipsum(
base_family = 'STSong'
)

在散点上添加tooltip

下面是将model变量设置为鼠标悬停在散点上显示的提示:

R R
1
2
3
4
5
my_gg <- g +
geom_point_interactive(
aes(tooltip = model, size = 2)
)
girafe(code = print(my_gg))

设置悬停效果

与data_id相关联的散点会在鼠标悬停时显示tooltip:

R
1
2
3
4
5
6
my_gg <- g + geom_point_interactive(
aes(tooltip = model,
data_id = model),
size = 2)

girafe(code = print(my_gg))

设置鼠标点击效果

R
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
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimes %>% head()

# 创建'onclick'列
crimes$onclick <- sprintf(
"window.open(\"%s%s\")",
"http://en.wikipedia.org/wiki/",
as.character(crimes$state)
)

gg_crime <- ggplot(
crimes,
aes(
x = Murder,
y = Assault,
color = UrbanPop
)) +
geom_point_interactive(
aes(
data_id = state,
tooltip = state,
onclick = onclick
),
size = 3
) +
scale_color_gradient(
low = '#999999',
high = '#FF3333'
) +
hrbrthemes::theme_ipsum(
base_family = 'STSong'
)

girafe(ggobj = gg_crime)

这里的散点点击之后会跳转至维基百科。

工具提示的位置

可以通过参数offx和offy设定水平和竖直方向偏移的像素量:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
theme_set(hrbrthemes::theme_ipsum(
base_family = 'STSongti'
))
dataset <- mtcars
dataset$carname <- row.names(dataset)
gg_point_1 <- ggplot(
dataset,
aes(x = disp,
y = qsec,
tooltip = carname,
data_id = carname,
color = wt)) +
geom_point_interactive(size = 3)
x <- girafe(ggobj = gg_point_1)
girafe_options(
x = x,
opts_tooltip(
offx = 20,
offy = 20
)
)

如果参数use_cursor_pos设置为FALSE的话,则不使用鼠标位置,tooltip将会固定为offx和offy:

R
1
2
3
4
5
6
girafe_options(
x = x,
opts_tooltip(offx = 60,
offy = 60,
use_cursor_pos = F)
)

tooltip的style

tooltip的style由css控制,常用的参数有:

  • background-color
  • color
  • border-style/border-width/border-color
  • width/height
  • padding:边距

例如设定背景透明和斜体:

R
1
2
3
4
5
6
7
tooltip_css <- "background-color:transparent;font-style:italic;"
girafe_options(
x = x,
opts_tooltip(
css = tooltip_css
)
)

再例如设定灰色背景、斜体、白色字体、边距为10px、边角半径为10px:

R
1
2
3
4
5
6
7
tooltip_css <- "background-color:gray;color:white;font-style:italic;padding:10px;border-radius:20px;"
girafe_options(
x = x,
opts_tooltip(
css = tooltip_css
)
)

使用元素的颜色填充tooltip

R
1
2
3
4
5
6
girafe_options(
x = x,
opts_tooltip(
use_fill = T
)
)

鼠标悬停效果

这里是指鼠标悬停在散点上散点的效果,同样使用css控制,常用的参数有:

  • fill:背景颜色
  • stroke:边界颜色
  • stroke-width:边界宽度
  • r:散点半径
R
1
2
3
4
girafe_options(
x = x,
opts_hover(css = "fill:red;stroke:green;r:10px;stroke-width:5px")
)

zoom效果

R
1
2
3
4
girafe_options(
x = x,
opts_zoom(max = 5)
)

toolbar:工具条

可以使用opts_toolbar()函数设定toolbae的位置:

R
1
2
3
4
5
girafe_options(
x = x,
opts_toolbar(position = 'bottomright'),
opts_zoom(max = 3)
)

添加图片下载

R
1
2
3
4
girafe_options(
x = x,
opts_toolbar(saveaspng = T)
)

多图联动

R
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
library(ggplot2)
library(ggiraph)
library(patchwork)

mtcars <- mtcars
mtcars$tooltip <- row.names(mtcars)

gg1 <- ggplot(mtcars) +
geom_point_interactive(
aes(x = drat,
y = wt,
color = qsec,
tooltip = tooltip,
data_id = tooltip),
size = 4
)

gg2 <- ggplot(mtcars) +
geom_point_interactive(
aes(x = qsec,
y = disp,
color = mpg,
tooltip = tooltip,
data_id = tooltip),
size = 4
)

# 使用cowplot拼图
# 这样可以实现双图联动效果
girafe(code = print(gg1 + gg2),
width_svg = 8,
height_svg = 4)

# patchwork拼图
girafe(code = print(gg1 + gg2),
width_svg = 8,
height_svg = 4)

创建一个ggiraph对象

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
dataset <- structure(
list(
qsec = c(16.46, 17.02, 18.61, 19.44, 17.02, 20.22),
disp = c(160, 160, 108, 258, 360, 225),
carname = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant"),
wt = c(2.62, 2.875, 2.32, 3.215, 3.44, 3.46)
),
row.names = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant"),
class = "data.frame"
)

gg_point <- ggplot(
data = dataset,
aes(wt, qsec, color = disp,
tooltip = carname,
data_id = carname)
) +
geom_point_interactive()

girafe(ggobj = gg_point, width = 0.9)

交互式柱形图

R
1
2
3
4
5
6
7
8
9
10
11
12
13
g <- ggplot(
data = mpg,
aes(x = class,
color = class,
fill = class,
tooltip = class,
data_id = class)) +
geom_bar_interactive() +
scale_color_brewer(palette = 'Set2') +
scale_fill_brewer(palette = 'Set2') +
theme(legend.position = 'none')

ggiraph(code = print(g))

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
dat <- data.frame(
name = c("David", "Constance", "Leonie"),
gender = c("Male", "Female", "Female"),
height = c(172, 159, 71))

g <- ggplot(
data = dat,
aes(
x = name,
y = height,
color = name,
fill = name,
tooltip = gender,
data_id = name
)
) +
geom_bar_interactive(stat = 'identity') +
scale_color_brewer(palette = 'Set2') +
scale_fill_brewer(palette = 'Set2') +
theme(legend.position = 'none')

ggiraph(code = print(g))

交互式箱线图

R
1
2
3
4
5
6
7
8
9
10
11
12
p <- ggplot(
data = mpg,
aes(x = class,
y = hwy,
fill = class,
tooltip = class)
) +
geom_boxplot_interactive() +
scale_fill_brewer(palette = 'Set2') +
theme(legend.position = 'none')

ggiraph(code = print(p))

R
1
2
3
4
5
6
7
8
9
10
11
12
p <- ggplot(
data = mpg,
aes(x = drv,
y = hwy,
tooltip = class,
fill = class)
) +
scale_fill_brewer(palette = 'Set2') +
geom_boxplot_interactive(outlier.color = 'red') +
guides(fill = 'none')

ggiraph(ggobj = p)

更多图层

参考Function reference • ggiraph package

自定义动画

悬停效果

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dataset <- mtcars
dataset$carname = row.names(mtcars)

gg <- ggplot(
data = dataset,
aes(wt, qsec,
color = disp,
tooltip = carname,
data_id = carname)
) +
geom_point_interactive()

x <- girafe(ggobj = gg)
girafe_options(
x = x,
opts_hover(css = "fill:wheat;stroke:orange;r:5pt;")
) %>% print()

选择效果

R
1
2
3
4
5
6
7
girafe_options(
x = x,
opts_selection(
type = "multiple",
css = "fill:red;stroke:gray;r:5pt;"
)
) %>% print()

其它

还有该包的shiny用法我没有看。

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×