在ggvis中使用layer_points + layer_boxplot时添加交互性

时间:2015-10-30 19:15:11

标签: r ggvis

在想要添加工具提示时,我无法让layer_points()layer_boxplot()一起工作。

require(ggvis)

### Example Data
mtc<-mtcars
mtc$label <- rownames(mtcars)   #unique Identifier


## Tooltip but not boxplot - works fine
mtc  %>% 
  ggvis(~cyl, ~qsec, key:= ~label) %>% 
  layer_points(size := 15) %>% 
  set_options(width = "auto") %>%
  add_tooltip(function(data) data$label)
})  

enter image description here

## No tooltip - boxplot + points  - works fine
mtc  %>% 
  ggvis(~cyl, ~qsec) %>% 
  layer_points(size := 15) %>% 
  layer_boxplots(size := 0)   %>%
  set_options(width = "auto")

enter image description here

## Attempt at combining boxplot, points, tooltip - fails
mtc  %>% 
  ggvis(~cyl, ~qsec, key:= ~label) %>% 
  layer_points(size := 15) %>% 
  layer_boxplots(size := 0)   %>%
  set_options(width = "auto") %>%
  add_tooltip(function(data) data$label)

#Error in eval(expr, envir, enclos) : object 'label' not found
#Plot is produced but no interactivity

有没有人知道如何将箱形图重叠到layer_points()加上工具提示(基本上是为了识别异常值是我的目标)。

另一个答案是如何为从layers_boxplot()

自动生成的异常值添加交互性

1 个答案:

答案 0 :(得分:1)

好像我已经解决了 - 他们key必须进入layer_points()

mtc  %>% 
  ggvis(~cyl, ~qsec) %>% 
  layer_points(key:= ~label, size := 15) %>% 
  layer_boxplots(size := 0)   %>%
  set_options(width = "auto") %>%
  add_tooltip(function(data) data$label)