How can I add a horizontal line in ggvis?

点点圈 提交于 2019-12-25 04:53:22

问题


I'm trying to learn ggvis, and I'm working on Boston as a tutorial. Basically, I'm trying to convert the ggplot that I worked on into ggvis on Boston data in R. It seems like I can't add horizontal mean line in ggvis. Although I found some hack after googling, but I still couldn't figure it out how I could work it out in my code. Here's my code:

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

so, that's the plot, and I want to add the mean line. This is what I tried:

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. 

This didn't produce what I wanted to. In fact, it gave me an error.

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

回答1:


Can't you just add a new column of the mean value into the boston data frame with dplyr? mutate(boston, line=mean("what you want the mean of")

then add %>% layer_paths(~x,~meanvalue,stroke:=black)



来源:https://stackoverflow.com/questions/31666210/how-can-i-add-a-horizontal-line-in-ggvis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!