Search code examples
rggvis

Change a single grid line color ggvis


I am using ggvis to create some plots. I am wondering if it is possible to change the color of only a single horizontal grid line. For instance, after running the code, I want to change the color of grid line with tick mark = 60 to red.

iris %>%
  ggvis(~Species, ~Petal.Width) %>%
  layer_bars() %>% 
  add_axis('x', title = "Species") 

I know adding a horizontal line with layer_lines is a way to do it, but in my real case, this way is not feasible. After some intensive search on google, I still cannot figure out how to do that. Thanks for your help in advance.


Solution

  • You can pass a named list of parameters to add_axis, that correspond with the vega options. In this case, you can make a regular y-axis grid and then add another with special properties. Make sure to use the scaled_value to get the proper positioning on the respective scale.

    iris %>%
      ggvis(~Species, ~Petal.Width) %>%
      layer_bars() %>%
      add_axis('x', title = "Species") %>%
      add_axis('y') %>%
      add_axis('y', properties = axis_props(
                        grid = list(y=scaled_value('y', 60),
                            stroke='red', strokeOpacity=0.1)))
    

    enter image description here