你描述label
在你的帖子中但是label
您的数据中缺少,因此我添加了一个label
列至PT
一些标签与您的评论中似乎建议的颜色相对应。PT
is now:
SHD_ID MEAS XAVG YAVG PPKLOW PPKUP PKSLOW PKSUP colour label
1 1L3P1 0.300018 -5774.5 94350.5 FALSE FALSE TRUE FALSE blue Fail
2 1LSP0 0.300018 14245.5 94350.5 FALSE FALSE TRUE FALSE blue Fail
3 1L802 600.000000 -25024.5 94350.5 FALSE FALSE FALSE TRUE green3 Mean
4 ILSOl 0.300018 -5004.5 94350.5 FALSE FALSE TRUE FALSE blue Fail
5 1LSOO 0.300018 15015.5 94350.5 FALSE FALSE TRUE FALSE blue Fail
6 1LSN2 600.000000 -24254.5 94350.5 FALSE FALSE FALSE TRUE green3 Mean
代码首先创建color_map
作为具有命名组件的向量来定义标签和颜色之间的映射。然后它创建绘图,为marker
财产color
基于color_map
and label
. The label
值也用于创建group
以便在图例中显示标签的颜色分配。我将标记做得更大,以便更容易看到颜色。
#
# create a map between colors and labels
# colors ending in numbers are not recognized by plot_ly
#
color_map <- c(Pass="yellow", High="red", Low= "cyan",
Sigma= "magenta", Mean='limegreen', Fail="blue", Median="violet")
#
# plot points using color_map to assign colors by label
# show label colors in legend
#
p <- plot_ly(PT,
type='scatter',
x=XAVG,
y=YAVG,
mode= "markers",
marker=list(size=10, bgcolor="#e5e5e5",
color=color_map[label]),
group = label,
zeroline=FALSE,
text=paste("MEAS = ", MEAS,"Head_ID = ",substr(SHD_ID,2,5)))