You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly绘图中marker.line.color与marker.line.width异常问题求助

Plotly标记边框颜色异常问题排查

问题描述

使用Plotly绘制散点图时出现标记边框颜色错误:预期标记点按Color列着色,标记边框的颜色和宽度按Selected列设置,但实际部分点的边框颜色不符合预期(例如左侧第一个点边框应为蓝色却显示粉色),该功能在部分数据集正常,部分数据集异常。

测试数据集

Dim_1Dim_2ColorText_IDSeq_typeSelected
10.83541-19.608079AH4ARAH4
4.38106-2.295184AH4BRAH4
22.15493-2.272569AH4CRUser selected
21.85514-3.964437AH4DRUser selected
22.079368.677288AH2ERAH2
17.56850-4.638592AH4FRAH4
1.21-4.592AH1GRAH1
4.21-5.592AH3HRAH3
4.21-21.592AH5IRAH5

原代码

BORDER_symbol=c("circle", "star-diamond")
names(BORDER_symbol)=c("R", "G")
Ex_colors=c( "#00798c", "#9e51bd","#9D5568" ,"#d1495b","#DF7C52" ,"#050609" ,"#ff4d6d" )
names(Ex_colors)=c("AH1", "AH2", "AH3", "AH4", "AH5","User selected", "Counterpart")
Ex_width=c(rep(3, 5),5,1)
names(Ex_width)=names(Ex_colors)

plot_ly(
  data = test,
  x =  ~ Dim_1,
  y = ~ Dim_2,
  color = ~ Color,
  colors = Ex_colors,
  opacity = 0.7,
  text =  ~ Text_ID,
  key = ~ Text_ID,
  type = 'scatter',
  mode = 'markers',
  hovertemplate = paste('<b>%{text}</b>'),
  
  marker = list(
    size = 7,
    line= list(color=(Ex_colors[match(test$Selected, names(Ex_colors))]),
               width=( Ex_width[match(test$Selected, names(Ex_width))]))
    
  )
)

错误现象

标记边框颜色错误,左侧第一个点边框应为蓝色却显示粉色

问题原因

Plotly的顶层color = ~Color参数用于按Color列对数据分组,会为每个分组统一应用标记样式(包括边框颜色),这会覆盖你手动为每个点设置的marker$line属性,导致部分点的边框颜色不符合预期。

修正方案

去掉顶层color参数,直接在marker中为每个点单独指定填充色、边框颜色和宽度:

BORDER_symbol=c("circle", "star-diamond")
names(BORDER_symbol)=c("R", "G")
Ex_colors=c( "#00798c", "#9e51bd","#9D5568" ,"#d1495b","#DF7C52" ,"#050609" ,"#ff4d6d" )
names(Ex_colors)=c("AH1", "AH2", "AH3", "AH4", "AH5","User selected", "Counterpart")
Ex_width=c(rep(3, 5),5,1)
names(Ex_width)=names(Ex_colors)

# 提前为每个点匹配对应的颜色和边框宽度
test$marker_fill <- Ex_colors[match(test$Color, names(Ex_colors))]
test$marker_line_color <- Ex_colors[match(test$Selected, names(Ex_colors))]
test$marker_line_width <- Ex_width[match(test$Selected, names(Ex_width))]

plot_ly(
  data = test,
  x =  ~ Dim_1,
  y = ~ Dim_2,
  opacity = 0.7,
  text =  ~ Text_ID,
  key = ~ Text_ID,
  type = 'scatter',
  mode = 'markers',
  hovertemplate = paste('<b>%{text}</b>'),
  
  marker = list(
    size = 7,
    color = ~marker_fill,
    line= list(color= ~marker_line_color,
               width= ~marker_line_width)
  )
)

Session信息

R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C               LC_TIME=es_ES.UTF-8        LC_COLLATE=es_ES.UTF-8     LC_MONETARY=es_ES.UTF-8    LC_MESSAGES=es_ES.UTF-8    LC_PAPER=es_ES.UTF-8       
 [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C        

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base      

other attached packages:
[1] plotly_4.10.1 ggplot2_3.4.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10          tidyr_1.3.0          shinyWidgets_0.7.6   digest_0.6.31        utf8_1.2.3           mime_0.12            R6_2.5.1             RSQLite_2.2.20       httr_1.4.4           
[10] pillar_1.8.1         rlang_1.0.6          lazyeval_0.2.2       scrypt_0.1.6         rstudioapi_0.14      data.table_1.14.6    jquerylib_0.1.4      blob_1.2.3           R.utils_2.12.2       
[19] R.oo_1.25.0          DT_0.27              shinyjs_2.1.0        htmlwidgets_1.6.1    bit_4.0.5            munsell_0.5.0        shiny_1.7.4          compiler_4.2.2       httpuv_1.6.8         
[28] pkgconfig_2.0.3      askpass_1.1          billboarder_0.4.0    htmltools_0.5.4      openssl_2.0.5        tidyselect_1.2.0     tibble_3.1.8         fansi_1.0.4          viridisLite_0.4.1    
[37] dplyr_1.1.0          withr_2.5.0          later_1.3.0          R.methodsS3_1.8.2    grid_4.2.2           jsonlite_1.8.4       xtable_1.8-4         gtable_0.3.1         lifecycle_1.0.3      
[46] DBI_1.1.3            magrittr_2.0.3       scales_1.2.1         cli_3.6.0            cachem_1.0.6         farver_2.1.1         promises_1.2.0.1     bslib_0.4.2          ellipsis_0.3.2       
[55] generics_0.1.3       vctrs_0.5.2          tools_4.2.2          bit64_4.0.5          glue_1.6.2           purrr_1.0.1          crosstalk_1.2.0      Peptides_2.4.4       yaml_2.3.7           
[64] fastmap_1.1.0        colorspace_2.1-0     shinymanager_1.0.410 bigassertr_0.1.6     shinydashboard_0.7.2 memoise_2.0.1        sass_0.4.5  

内容的提问来源于stack exchange,提问作者R. Garcí­a

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:47:06