向 R 散点图添加多条散点边框线

问题描述 投票:0回答:1

我正在尝试使用

R
plotly
在此示例中生成散点图
data.frame
:

set.seed(1)
library(dplyr)
library(plotly)

df <- data.frame(x = runif(6, -3, 3), y = 1:6, group = sample(c("a", "b"), 6, replace = T), class = sample(c(T, F), 6, replace = T))

我愿意:

  1. df$group
    对点内部进行颜色编码。
  2. df$class
    对点的边框进行颜色编码。

我的问题是如何实现#2? 我知道如何用单一颜色为边框着色:

plotly::plot_ly(type = "scatter",mode = "markers", x = df$x, y = df$y, color = df$group, marker = list(size = 13, line = list(color = 'black', width = 2)))

但是尝试类似的事情:

df <- df %>% dplyr::mutate(border.color = ifelse(class, 'black', 'green'))
plotly::plot_ly(type = "scatter",mode = "markers", x = df$x, y = df$y, color = df$group, marker = list(size = 13, line = list(color = df$border.color, width = 2)))

不起作用:

有什么想法吗?

r plotly border scatter-plot color-scheme
1个回答
0
投票

您需要区分映射(根据变量改变参数)和设置值。如果您想更改点的边界,您可以将变量映射到

stroke
,如下所示:

library(dplyr)
library(plotly)

set.seed(1)
df <- data.frame(x = runif(6, -3, 3),
                 y = 1:6,
                 group = sample(c("a", "b"), 6, replace = T),
                 class = sample(c(T, F), 6, replace = T))

plotly::plot_ly(data = df,
                type = "scatter",
                mode = "markers",
                x = ~x,
                y = ~y,
                color = ~group,
                stroke = ~class,
                strokes = c(`TRUE`="black",`FALSE`="green"),
                marker = list(size = 13, line = list(width = 2)))

© www.soinside.com 2019 - 2024. All rights reserved.