在ggplot2可视化文件中超链接文本 [英] Hyperlinking text in a ggplot2 visualization
本文介绍了在ggplot2可视化文件中超链接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前,如果我想在R中的表中显示数据,我可以通过markdown,html href或LaTeX href超链接文本.通常可以很好地访问有关特定元素的更多信息,而又不会使表格混乱.
Currently if I want to show data in a table in R I can hyperlink text via markdown, html href, or LaTeX href. This is often nice for giving access to more info about a particular element w/o cluttering the table.
如何在使用ggplot2制作的可视化文件中提供相同类型的超链接文本?
例如,如果我绘制此图:
So for example if I make this plot:
使用下面的代码,如何使轴文本超链接到对应的Wikipedia页面?
With the code below, how can I make the axis text hyperlink to the wikipedia pages that correspond?
library(tidyverse)
mtcars %>%
rownames_to_column('car') %>%
slice(5:8) %>%
mutate(
link = c(
'https://de.wikipedia.org/wiki/AMC_Hornet',
'https://en.wikipedia.org/wiki/Plymouth_Valiant',
'https://en.wikipedia.org/wiki/Plymouth_Duster',
'https://en.wikipedia.org/wiki/Mercedes-Benz_W123'
)
) %>%
ggplot(aes(x = mpg, y = car)) +
geom_point(size = 2)
推荐答案
@ user20650这是一个'gridSVG'解决方案:
@user20650 Here is a 'gridSVG' solution:
library(tidyverse)
links <- c('https://en.wikipedia.org/wiki/Plymouth_Duster',
'https://de.wikipedia.org/wiki/AMC_Hornet',
'https://en.wikipedia.org/wiki/Mercedes-Benz_W123',
'https://en.wikipedia.org/wiki/Plymouth_Valiant')
mtcars %>%
rownames_to_column('car') %>%
slice(5:8) %>%
mutate(
link = links
) %>%
ggplot(aes(x = mpg, y = car)) +
geom_point(size = 2)
library(grid)
## Force 'grid' grobs from 'ggplot2' plot
grid.force()
## List all grobs in plot
grid.ls()
## Find the grobs representing the text labels on the axes
tickLabels <- grid.grep("axis::text", grep=TRUE, global=TRUE)
## Check which one is the y-axis
lapply(tickLabels, function(x) grid.get(x)$label)
## Add hyperlinks to the axis tick labels
library(gridSVG)
grid.hyperlink(tickLabels[[1]],
href=links,
group=FALSE)
## Export to SVG (and view in a browser)
grid.export("linked-plot.svg")
这篇关于在ggplot2可视化文件中超链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文