【问题标题】:Hyperlinking text in a ggplot2 visualizationggplot2 可视化中的超链接文本
【发布时间】:2023-03-28 09:23:02
【问题描述】:

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

如何在用 ggplot2 制作的可视化中给出相同类型的超链接文本?

例如,如果我制作这个情节:

使用下面的代码,如何将轴文本超链接到对应的维基百科页面?

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)

【问题讨论】:

  • 用什么设备保存?显然 JPEG 等已经出局,但 PDF 可能通过网格实现。对于网络,可能通过 D3.js/SVG 画布,可能使用ggplotly 进行初始转换。不过,我认为任何选择都不会简单。
  • 我认为很难将单独的链接应用到轴文本,因为它们是(我认为)一个 grob,而不是单独的,但 gridSVG 包显示在浏览器中查看时添加到 grobs 的链接的方法。因此,您可以对点、标签等使用单独的 grobs,并添加指向这些内容的链接(我已经使用了这个 here
  • @alistaire 我想在 knitr/rmarkdown 到 pdf_document 中使用它
  • 关于 PDF,请参阅我在 lukeA 的回答中的评论

标签: r ggplot2


【解决方案1】:

这是我使用的一个选项。

你的例子:

library(tidyverse)
library(xml2)
df <- 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'
    )
  ) 
p <- df %>%
  ggplot(aes(x = mpg, y = car)) +
  geom_point(size = 2) 

然后:

ggsave( tf1 <- tempfile(fileext = ".svg"), p)
links <- with(df, setNames(link, car))

xml <- read_xml(tf1)
xml %>%
  xml_find_all(xpath="//d1:text") %>% 
  keep(xml_text(.) %in% names(links)) %>% 
  xml_add_parent("a", "xlink:href" = links[xml_text(.)], target = "_blank")
write_xml(xml, tf2 <- tempfile(fileext = ".svg"))

如果您在浏览器中打开tf2

然后,您可以将其转换为 pdf(取自以下@captcoma 的评论):

library(rsvg)
rsvg_pdf(tf2, "out.pdf")

【讨论】:

  • 哇,这太棒了!
  • 我发现它很有用,但是我不知道它是否适用于所有浏览器。我在 Windows 上使用 Chrome 56 和 Firefox 50.1 进行了尝试...
  • 这太棒了。适用于 MacOS 上的 Firefox、Safari、Chrome、Opera 和 Vivaldi。
  • 这太棒了!如果您想将其作为 PDF 格式,只需添加 library(rsvgrsvg_pdf(tf2, "out.pdf")
  • @captcoma 谢谢我也将此添加到主要答案中。不错!
【解决方案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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多