如何在ggvis中添加水平线?

时间:2015-07-28 02:01:05

标签: r ggvis

我正在尝试学习ggvis,而且我正在波士顿学习作为教程。基本上,我试图将我工作的ggplot转换为R中波士顿数据的ggvis。似乎我不能在ggvis中添加水平均值线。虽然我在谷歌搜索后发现了一些黑客攻击,但我仍然无法弄清楚如何在我的代码中解决这个问题。这是我的代码:

library(dplyr, warn.conflicts = FALSE)
library(ggvis)
Boston %>%
  ggvis(~chas, ~log(medv), fill=~chas, opacity := 0.8) %>%
  layer_boxplots(size := 10)

所以,这是情节,我想添加平均线。这就是我试过的:

data_line = data.frame(
   x_rng = c(0,1),   #this is the part that I couldn't figure out.
   y_rng = c(3,3)
)
layer_lines(~x_rng, ~y_rng, data=data_line) #this is what I added to the code above. 

这并没有产生我想要的东西。事实上,它给了我一个错误。

## Error in new_prop.default(x, property, scale, offset, mult, env, event, : 
## Unknown input to prop: c(0, 1)c(3, 3)

1 个答案:

答案 0 :(得分:1)

难道你不能只用dplyr将新的平均值列添加到波士顿数据框中吗? mutate(boston, line=mean("what you want the mean of")

然后添加%>% layer_paths(~x,~meanvalue,stroke:=black)