简体   繁体   English

为 ggiraph 的交互式工具提示格式化表格的最简单方法

[英]Easiest way to format table for interactive tooltip for ggiraph

I have this basic example below.我在下面有这个基本示例。 As you will see it is an interactive plot. What I am hoping to achieve at the moment is to take the dummy_data and make it a formatted table for the tooltip when you hover over a datapoint.正如您将看到的那样,它是一个交互式 plot。我目前希望实现的是获取dummy_data并在您 hover 通过数据点时将其作为工具提示的格式化表格。 I am going to have different sets for points but I just want this one example to work for all and then I can change the data.我将有不同的点集,但我只想让这个示例适用于所有人,然后我可以更改数据。

As you will see in the tooltip = argument it would need to be included there.正如您将在tooltip =参数中看到的那样,它需要包含在此处。 I am just not sure if there is an easy way to pass this table as it is printed in there with some css to format it easily.我只是不确定是否有一种简单的方法来传递此表,因为它在其中打印了一些 css 以便轻松格式化。 I want it to look as good or better than dummy_data printed to console essentially.我希望它看起来与打印到控制台的dummy_data一样好或更好。

I hope this is clear enough?我希望这已经足够清楚了吗? Please let me know if I can clarify.如果我可以澄清,请告诉我。 See reprex below.请参阅下面的代表。

library(ggplot2)
library(ggiraph)
#> Warning: package 'ggiraph' was built under R version 4.1.2


dataset <- mtcars

dummy_data <- 
  data.frame(
    stringsAsFactors = FALSE,
    Feature = c("FeatureA","FeatureB",
                "FeatureC","FeatureD","FeatureE","FeatureF","FeatureG",
                "FeatureH","FeatureI","FeatureJ","FeatureK","FeatureL",
                "FeatureM","FeatureN","FeatureO","FeatureP","FeatureQ",
                "FeatureR","FeatureS","FeatureT","FeatureU"),
    Rating = c(1L,1L,1L,1L,3L,3L,4L,1L,
               4L,1L,4L,1L,1L,1L,1L,4L,1L,1L,2L,3L,2L)
  )

dummy_data
#>     Feature Rating
#> 1  FeatureA      1
#> 2  FeatureB      1
#> 3  FeatureC      1
#> 4  FeatureD      1
#> 5  FeatureE      3
#> 6  FeatureF      3
#> 7  FeatureG      4
#> 8  FeatureH      1
#> 9  FeatureI      4
#> 10 FeatureJ      1
#> 11 FeatureK      4
#> 12 FeatureL      1
#> 13 FeatureM      1
#> 14 FeatureN      1
#> 15 FeatureO      1
#> 16 FeatureP      4
#> 17 FeatureQ      1
#> 18 FeatureR      1
#> 19 FeatureS      2
#> 20 FeatureT      3
#> 21 FeatureU      2

dataset$carname <- row.names(dataset)
dataset$tooltip <- paste(dummy_data)



gg_scatter <- ggplot(dataset, 
                     aes(x = disp, y = qsec, tooltip = 
                           paste0(
                             "<div class='header' checked>
                           <p>Ready to take ",tooltip,"? If so</p>
                           <a href='shiny.rstudio.com/tutorial'>Click Here!</a>
                           </div>"), 
                         data_id = carname, color= wt) ) + 
  geom_point_interactive(size=3) + 
  labs(title = "mouse over points") + 
  theme_minimal() + theme(
    plot.background = element_blank(),
    panel.background = element_blank()
  )



girafe(ggobj = gg_scatter, 
       options = list(
         opts_sizing(rescale = TRUE, width = .7) )
)

Created on 2022-04-27 by the reprex package (v2.0.0)reprex package (v2.0.0) 创建于 2022-04-27

You can pass HTML code to the tooltip, so one way would be to create an HTML version of your table using knitr::kable , get the HTML table code and add it to your dataframe and then use as tooltip.您可以将 HTML 代码传递给工具提示,因此一种方法是使用knitr::kable创建表格的 HTML 版本,获取 HTML 表格代码并将其添加到您的 dataframe,然后用作工具提示。

library(ggplot2)
library(ggiraph)

dataset <- mtcars

# add tooltip, using only first 10 rows to avoid showing too much text
dataset$tooltip <- knitr::kable(dummy_data[1:10, ], format = "html")

gg_scatter <- ggplot(dataset, 
                     aes(x = disp, y = qsec, 
                         tooltip = tooltip, color= wt) ) + 
    geom_point_interactive(size=3) 

girafe(ggobj = gg_scatter)

输出

For more customisation for the tooltip table, instead of knitr::kable , you can use some other package that allows creating more customised HTML tables.要对工具提示表进行更多自定义,而不是knitr::kable ,您可以使用其他一些 package 来创建更多自定义的 HTML 表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM