简体   繁体   English

如何在ggvis中为图例添加特定点?

[英]How to add a specific point to legend in ggvis?

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. 本质上,我有一个要在ggvis中绘制的数据框,但我想尝试为几个特定的​​点着色并添加表格以标记这些点的含义。 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. 本质上,我想为该点和其他三个点添加颜色,以及一个简单的图例,以标识这些点分别是各自的颜色,而将其他点保留为黑色。

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 . 你可以控制你在使用的颜色填充scale_nominal ,可以更改图例的外观与add_legend

Here is an example using mtcars . 这是使用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" )

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM