鼠标悬停时Rstudio闪亮的ggvis工具提示

时间:2014-07-25 15:46:39

标签: r shiny ggvis

在下面的示例中,我有一个交互式闪亮的ggvis图,但我添加了一个long列,这是一个长字符串,出于某种原因,我的悬停弹出窗口显示wt和{{ 1}}但不显示mpg

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

有什么想法吗?

long

2 个答案:

答案 0 :(得分:11)

您需要在提供给long的匿名函数中添加data$long当前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")]
  })
})
)

enter image description here

答案 1 :(得分:11)

我一直对此感到困惑。似乎您只能获得ggvis()数据中的 TOOLTIP 信息。因此,如果你有ggvis(~wt,~mp),你可以在工具提示中显示wt和mp。如果你有ggvis(~wt,~mpg,fill = ~long)你可以在工具提示中显示wt,mp,long。 或者是层_的数据。(fill = ~long,stroke = ~name,strokeWidth:= 0)。 (我从行名中创建了〜名称)你需要抑制笔画和图例以避免视觉冲击:hide_legend(&#34; stroke&#34;)%&gt;%

据我所知,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,笔划,填充,不透明度,形状,键(必须是唯一值),文本,字体,字体大小等等或其中一个层_.....属性。

如果可以将数据帧数据添加到未在绘图中使用的ggplot但仅在 TOOLTIP 中可用,则会很好。如果我找到了一种方式,我也会在这里发布

我添加了隐形名称信息,只有这样才能在工具提示中显示(基于前面的示例):

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图中的所有数据: (另见:Add data to ggvis tooltip that's contained in the input dataset but not directly in the vis

关键是独一无二!!!链接到另一个数据集和一个外部的函数 ggvis(),它返回要在 TOOLTIP中显示的数据和信息。

我将代码更改为:

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
  }
})

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

enter image description here

如果你能以这种方式结合两者(Hover - &gt;摘要信息&amp;&amp; Click - &gt;所有信息),那么

和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
  }
})

要完成!

甚至完成 - &gt;工具提示只是HTML代码字符串 - &gt;所以在某种程度上你可以创建你想要显示的任何HTML页面。你只是不能指出它,因为一旦你离开这一点它就会消失! (但您可以在同一点进行点击操作以补充(例如重定向页面)悬停操作。)

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

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

(对不起,答案很长)