问题
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