0

Essentially I have a data frame that I am plotting in ggvis but I want to try to color a couple specific points and add a table to mark what those points mean. So far I am able to add color to those points but I am unable to create the legend identifying that point.

g1 <- subset(ex, p == 0.10) 

ex %>% ggvis(x = ~p, y = ~Pa_Achieved) %>% layer_lines() %>% 
   layer_points() %>%  layer_points(data = g1, fill := "red") %>%
  add_axis("x", title ="p") %>% 
  add_axis("y", title = "Pa",
           properties=axis_props(labels=list(fontSize=12), 
                                 title=list(fontSize=12,dy=-25))) %>% 
  add_title(title = "Operating Characteristic Curve", 
            properties = axis_props(title=list(fontSize=20)))

Essentially I would like to add color to this and three other points, and a simple legend identifying what those points are their respective colors and leaving the other points black.

Imconfused
  • 31
  • 1
  • 5
  • Please, add a reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Lstat Jun 19 '17 at 05:24

1 Answers1

0

To make a legend from constants instead of a variable, you will need = ~ instead of :=. You control which colors you use for the fill in scale_nominal and can change how the legend looks with add_legend.

Here is an example using mtcars.

mtcars %>% 
     ggvis(x = ~mpg, y = ~wt) %>% 
     layer_points() %>%  
     layer_points(data = subset(mtcars, mpg == 33.9), fill = ~"mpg 33.9") %>%
     layer_points(data = subset(mtcars, mpg == 32.4), fill = ~"mpg 32.4") %>%
     scale_nominal("fill", range = c("red", "blue") ) %>%
     add_legend("fill", title = "groups" )

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118