我已经使用漂亮的 ggiraph 包interactive legend
中的功能构建了一个闪亮的应用程序。scale_fill_manual_interactive
但是,我想将onclick
事件中的信息(男性或女性,取决于选择)打印到控制台。这是代码片段。
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
gender = c( "Male", "Female", "Male", "Male", "Male" ),
height = c(169, 160, 171, 172, 171 ) )
p <- ggplot(dat, aes( x = name, y = height, fill = gender,
data_id = name ) ) +
geom_bar_interactive(stat = "identity")
p3 <- p +
scale_fill_manual_interactive(
name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) { as.character(breaks)},
tooltip = function(breaks) { as.character(breaks)},
onclick = function(breaks) { cat( as.character(breaks)) }
)
x <- girafe(ggobj = p3)
x <- girafe_options(x,
opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)
现在它只打印整个列表
Female Male
而不是只选择一个。