gpt4 book ai didi

r - 在ggplot2可视化文件中超链接文本

转载 作者:行者123 更新时间:2023-12-03 07:29:04 27 4
gpt4 key购买 nike

当前,如果我想在R中的表中显示数据,我可以通过markdown,html href或LaTeX href超链接文本。通常可以很好地访问有关特定元素的更多信息,而又不会使表格困惑。

如何在用ggplot2制作的可视化文件中提供相同种类的超链接文本?

因此,例如,如果我绘制此图:

enter image description here

使用下面的代码,如何使轴文本超链接到对应的Wikipedia页面?

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'解决方案:

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")

enter image description here

关于r - 在ggplot2可视化文件中超链接文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com