当鼠标悬停在数据点R上方时显示放大图片

时间:2019-01-30 20:29:18

标签: r image ggplot2 mouse

我正在尝试绘制一个散点图,当鼠标悬停在这些点之一上时,将出现一个对应于数据中URL的图像。

R中可能吗?似乎在python中是可能的...

谢谢

1 个答案:

答案 0 :(得分:1)

以下是使用 Highchater 软件包的解决方案:

library(highcharter)

df <- data.frame(x = c(1, 2, 3, 4),
                 y = rep(0, 4),
                 package = c("dplyr", "shiny", "purrr", "stringr"),
                 urlimage = c("https://github.com/rstudio/hex-stickers/raw/master/PNG/dplyr.png",
                              "https://github.com/rstudio/hex-stickers/raw/master/PNG/shiny.png",
                              "https://github.com/rstudio/hex-stickers/raw/master/PNG/purrr.png",
                              "https://github.com/rstudio/hex-stickers/raw/master/PNG/stringr.png"))

hover_info <- tags$tr(
  tags$th("Package"),
  tags$td(paste0("{point.package}")),
  tags$img(src = "{point.urlimage}", width = "125px", height = "125px"))  %>%
  as.character()

highchart() %>%
  hc_add_series(data = df, 
                mapping = hcaes(x = x, y = y), 
                type = "scatter",
                marker = list(radius = 5, symbol = "circle")) %>%
  hc_tooltip(
    useHTML = TRUE,
    headerFormat = "<table>",
    pointFormat = hover_info,
    footerFormat = "</table>"
  )

输出:

enter image description here