ggiraph:从选择框中选择点(按名称)

huangapple go评论72阅读模式
英文:

ggiraph: select points from selection box (by name)

问题

ggiraph在将ggplot2图形转换为交互式htmlwidget方面比plotly::ggplotly()产生的问题要少得多。然而,在ggiraph中,有一个关键特性我在plotly中很想要:通过名称选择点,在选择框中实现(通过plotly::highlight()中的selectize参数)。

以下是您用plotly实现的代码示例:

library(plotly)
data("ChickWeight")
cw <- highlight_key(ChickWeight, ~Chick, group = "Chicken")
p <-  ggplot(cw, aes(x = Time, y = weight, col = Diet, group = Chick)) +
  geom_line() + geom_point() 
pp <- ggplotly(p, tooltip = c("Chick"))
highlight(pp, on = "plotly_hover", selectize = TRUE)

您是否有办法在ggiraph中实现这一功能(而不创建Shiny应用程序并运行实时R会话)?

到目前为止,我使用以下代码:

library(ggiraph)
set_girafe_defaults(opts_hover_inv = opts_hover_inv(css = "opacity:0.1;"))
p <- ggplot(ChickWeight, aes(x = Time, y = weight,
                              colour = Diet, group = Chick,
                              tooltip = Chick, data_id = Chick)) +
  geom_line_interactive() + geom_point_interactive()
girafe(ggobj = p)

感谢您的帮助!

英文:

ggiraph creates much less issues converting a ggplot2 graphic to an interactive htmlwidget compared to plotly::ggplotly(). However, there is one key feature of plotly which I am missing in ggiraph: Selecting points by name in a selection box (via argument selectize in plotly::highlight())

library(plotly)
data(&quot;ChickWeight&quot;)
cw &lt;- highlight_key(ChickWeight, ~Chick, group = &quot;Chicken&quot;)
p &lt;-  ggplot(cw, aes(x = Time, y = weight, col = Diet, group = Chick)) +
  geom_line() + geom_point() 
pp &lt;- ggplotly(p, tooltip = c(&quot;Chick&quot;))
highlight(pp, on = &quot;plotly_hover&quot;, selectize = TRUE)

ggiraph:从选择框中选择点(按名称)

Is there a way I can achieve this with ggiraph (without creating a shiny app and running a live R session)?

Code I use so far:

library(ggiraph)
set_girafe_defaults(opts_hover_inv = opts_hover_inv(css = &quot;opacity:0.1;&quot;))
p &lt;-  ggplot(ChickWeight, aes(x = Time, y = weight,
                              colour = Diet, group = Chick,
                              tooltip = Chick, data_id = Chick)) +
  geom_line_interactive() + geom_point_interactive()
girafe(ggobj = p)

Thanks for your help!

答案1

得分: 1

没有闪亮,就没有可用的交互式框,但您可以使用以下代码初始化一个 girafe 输出,带有选择:

library(ggplot2)
library(ggiraph)
set_girafe_defaults(opts_hover_inv = opts_hover_inv(css = "opacity:0.1;"))
p <- ggplot(ChickWeight, aes(x = Time, y = weight,
                              colour = Diet, group = Chick,
                              tooltip = Chick, data_id = Chick)) +
  geom_line_interactive() + geom_point_interactive()
girafe(ggobj = p, options = list(
  opts_selection(
    selected = c("47", "48"), 
    css = girafe_css(css = "", point = "fill:red;r:5px;", 
                     area = "", line = "stroke:black;stroke-width:3px;"),
    type = "multiple", 
    only_shiny = FALSE)
))

ggiraph:从选择框中选择点(按名称)

英文:

There is no interactive box available without shiny but you can init a girafe output with a selection:

library(ggplot2)
library(ggiraph)
set_girafe_defaults(opts_hover_inv = opts_hover_inv(css = &quot;opacity:0.1;&quot;))
p &lt;-  ggplot(ChickWeight, aes(x = Time, y = weight,
                              colour = Diet, group = Chick,
                              tooltip = Chick, data_id = Chick)) +
  geom_line_interactive() + geom_point_interactive()
girafe(ggobj = p, options = list(
  opts_selection(
    selected = c(&quot;47&quot;, &quot;48&quot;), 
    css = girafe_css(css = &quot;&quot;, point = &quot;fill:red;r:5px;&quot;, 
                     area = &quot;&quot;, line = &quot;stroke:black;stroke-width:3px;&quot;),
    type = &quot;multiple&quot;, 
    only_shiny = FALSE)
))

ggiraph:从选择框中选择点(按名称)

huangapple
  • 本文由 发表于 2023年7月3日 15:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定