0

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)
Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
user1828605
  • 1,723
  • 1
  • 24
  • 63
  • Oh, I guess I didn't see the black line on the plot. I added `layer_lines(y=mean(log(medv)), stroke:= "red")`, then it showed up. Is there another way to do this? – user1828605 Jul 28 '15 at 03:16

1 Answers1

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)

aosmith
  • 34,856
  • 9
  • 84
  • 118
James Maine
  • 289
  • 2
  • 10
  • I keep getting `length(x) not equal to 1`. Why? – user1828605 Jul 28 '15 at 04:14
  • i hope you fiddled around with your syntax and got it to work. Just say the mean value is 3.5, i'm going to guess you put in ~3.5 as opposed to ~meanvalue which would return the col of the df which is just 3.5 repeating itself have you tried your code with one of the datasets with rStudio, it providing a reproducable example would greatly help in finding a solution Good luck – James Maine Aug 04 '15 at 08:06
  • yes I did get it working, after a couple hours of fiddling around. Sorry, I forgot to change the status to answered. – user1828605 Aug 04 '15 at 14:09