0

我已经使用漂亮的 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

而不是只选择一个。

4

2 回答 2

0

onclick 元素需要一个 javascript 指令。这也意味着这将存在于您的浏览器中但不在 R 中,以下是自定义 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) { sprintf("console.log(\"%s\")", 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)

在此处输入图像描述

于 2019-09-30T15:43:41.243 回答
0

这应该回答你的问题。它将在 R 控制台中打印最后一次单击的键。一个响应值可用于获取点击键,它的 ID 是plot_key_selected:ggiraph ID +_key_selected

library(shiny)
library(ggplot2)
library(ggiraph)

gg <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    geom_point_interactive( size = 3 ) + theme_minimal() +
    scale_colour_manual_interactive(
        data_id = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
        tooltip = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
        values = c("setosa" = "purple", "versicolor" = "blue", "virginica" = "darkgreen")
        )

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
        ),
        mainPanel(
            ggiraph::ggiraphOutput("plot")
        )
    )
)

server <- function(input, output) {
    output$plot <- renderggiraph({
        x <- girafe(code = print(gg), width_svg = 6, height_svg = 8)
        x
    })
    observe({
        print(input$plot_key_selected)
    })
}

shinyApp(ui = ui, server = server)
于 2019-09-30T20:09:55.357 回答