7

在下面的示例中,我有一个交互式闪亮 ggvis 图,但我添加了long一个长字符串列,并且由于某种原因,我的悬停弹出窗口显示wtmpg不显示long

此外,如果图例中的元素列表太长,它们会隐藏在图的右下角。有没有办法将这些堆叠在图例的几列中?

有任何想法吗?

# ui.R
library(ggvis)
shinyUI(pageWithSidebar(
  div(),
  sidebarPanel(
    sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
                value = 10, step = 1),
    uiOutput("plot_ui")
  ),
  mainPanel(
    ggvisOutput("plot"),
    tableOutput("mtc_table")
  )
))


# server.R
library(shiny)
library(ggvis)
shinyServer(function(input, output, session) {
  # A reactive subset of mtcars
  mtc <- reactive({
      data = mtcars[1:input$n, ]
      data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
      data
  })
  # A simple visualisation. In shiny apps, need to register observers
  # and tell shiny where to put the controls
  mtc %>%
    ggvis(~wt, ~mpg) %>%
    layer_points(fill = ~factor(long)) %>%
    add_tooltip(function(data){paste0("Wt: ", data$wt, "<br>", "Mpg: ",as.character(data$mpg), "<br>", "String: ", as.character(data$long))}, "hover") %>%
    bind_shiny("plot", "plot_ui")

   output$mtc_table <- renderTable({
     mtc()[, c("wt", "mpg", "long")]
   })
})
4

2 回答 2

11

您需要在提供给的匿名函数中添加long当前data$long为 null的键add_tooltip

library(shiny)
library(ggvis)

runApp(list(ui = pageWithSidebar(
  div(),
  sidebarPanel(
    sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
                value = 10, step = 1),
    uiOutput("plot_ui")
  ),
  mainPanel(
    ggvisOutput("plot"),
    tableOutput("mtc_table")
  )
)
, server= function(input, output, session) {
  # A reactive subset of mtcars
  mtc <- reactive({
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
    data
  })
  # A simple visualisation. In shiny apps, need to register observers
  # and tell shiny where to put the controls
  mtc %>%
    ggvis(~wt, ~mpg, key:= ~long) %>%
    layer_points(fill = ~factor(long)) %>%
    add_tooltip(function(data){
      paste0("Wt: ", data$wt, "<br>", "Mpg: ",as.character(data$mpg), "<br>", "String: ", as.character(data$long))
    }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  output$mtc_table <- renderTable({
    mtc()[, c("wt", "mpg", "long")]
  })
})
)

在此处输入图像描述

于 2014-07-25T15:59:07.583 回答
11

我也一直在困惑这个问题。似乎您只能获取ggvis () 数据中的 TOOLTIP 信息。所以如果你有 ggvis(~wt, ~mp) 你可以在工具提示中显示 wt 和 mp。如果您有 ggvis(~wt, ~mpg, fill= ~long) 您可以在工具提示中显示 wt, mp , long。或 layer_.....(fill = ~long ,stroke = ~name, strokeWidth := 0) 中的数据。(我从行名中创建了~name)您需要抑制笔划和图例以避免视觉影响: hide_legend("stroke") %>%

据我所知,ggvis 将工具提示中显示的信息量限制为包含的内容(很容易理解)

如果我们查看文档:

***Usage***
ggvis(data = NULL, ..., env = parent.frame())

***Arguments***
data A data object.

... Property mappings. If not named, the first two mappings are taken to be x and y. 

Common properties are x, y, stroke, fill, opacity, shape

env Environment in which to evaluate properties.

所以我们可以添加 x、y、描边、填充、不透明度、形状、键(必须是唯一值)、文本、字体、字体大小等或 layer_..... 属性之一中的任何内容。

如果可以将数据框数据添加到图中未使用但仅在工具提示中可用的 ggplot 中,那就太好了。如果我找到一种方法,我也会在这里发布

我以这种方式添加了仅在工具提示中可见的不可见名称信息(基于前面的示例):

mtc <- reactive({
  if(length(input$n)>0){
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and     ",data$gear," gears and ",data$carb, " carburators"))
    data$name <- rownames(data) 

  data %>%
    ggvis(~wt, ~mpg) %>%
    layer_points(fill = ~long ,stroke = ~name, strokeWidth := 0) %>%
    hide_legend("stroke") %>%
    add_tooltip(function(dataT){
      paste0("Car: ",dataT$name, "<br>", "Wt: ", dataT$wt, "<br>",
      "Mpg: ",as.character(dataT$mpg), "<br>", "String: ", as.character(dataT$long))
    }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  data
  }
})

非常欢迎提出更好、更稳定的解决方案的建议!;^)


好的,我找到了一种方法来显示工具提示中不在 ggvis 图中的所有数据:(另请参阅:将数据添加到包含在输入数据集中但不直接在 vis 中的 ggvis 工具提示

关键是独一无二的!!!链接到另一个数据集和ggvis()之外的函数,该函数返回要在工具提示中显示的数据和信息

我将代码更改为:

mtc <- reactive({
  if(length(input$n)>0){
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
    data$name <- rownames(data) 


  all_values <- function(x) {
    if(is.null(x)) return(NULL)
    row <- data[data$name == x$name, ]
    paste0(names(row), ": ", format(row), collapse = "<br />")
  }

  data %>%
    ggvis(~wt, ~mpg, key := ~name) %>%
    layer_points(fill = ~long) %>%

    add_tooltip(all_values, "hover")  %>%
#       add_tooltip(function(dataT){
#       paste0("Car: ",dataT$name, "<br>", "Wt: ", dataT$wt, "<br>", "Mpg: ",as.character(dataT$mpg), "<br>", "String: ", as.character(dataT$long))
#       }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  data
  }
})

这样您就可以以任何您想要的方式更改工具提示信息!

在此处输入图像描述


如果您可以以这种方式将两者结合起来(悬停 -> 汇总信息 && 单击 -> 全部信息),则为 TOP 事情:

mtc <- reactive({
  if(length(input$n)>0){
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
    data$name <- rownames(data) 


  all_values <- function(x) {
    if(is.null(x)) return(NULL)
    row <- data[data$name == x$name, ]
    paste0(names(row), ": ", format(row), collapse = "<br />")
  }

  data %>%
    ggvis(~wt, ~mpg, key := ~name) %>%
    layer_points(fill = ~long) %>%

    add_tooltip(all_values, "click")  %>%
    add_tooltip(function(dataT){
      paste0("Car: ",dataT$name, "<br>", "Wt: ", dataT$wt, "<br>", "Mpg: ",as.character(dataT$mpg), "<br>", "String: ", as.character(dataT$long))
      }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  data
  }
})

只为完整!

更完善 -> 工具提示只是作为字符串的 HTML 代码 -> 因此,您可以通过某种方式创建您想要显示的任何 HTML 页面。你只是不能指向它,因为一旦你离开点它就会消失!(但您可以在同一点进行单击操作来补充(例如重定向页面)悬停操作。)

只是工具提示中图像的最后一个简短示例:

  add_tooltip(function(img){'<img src="pic_mountain.jpg" alt="Mountain View" style="width:100px;height:100px;">'}, "hover") %>%

(对不起,答案很长)

于 2015-08-05T13:58:42.907 回答