英文:
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("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)
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 = "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)
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)
))
英文:
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 = "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)
))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论