首页 > 解决方案 > 当列包含“NA”时如何使用 Plotly 修复不正确的颜色

问题描述

当列包含“NA”值时,Plotly 会更改我为线条和标记分配的颜色。有想法该怎么解决这个吗?谢谢!(我正在使用 R 版本 '3.6.0 (2019-04-26)' 和 Plotly 版本 '4.9.0')

第一张图的数据包含绘制在 y 轴上的“trust_ecb”列的一些“NA”值。线条和标记的颜色不正确,即与代码中指定的颜色不同。

但是,当我将“NA”值重新编码为“70”并绘制图形时,颜色是正确的。

用不正确的颜色绘图('trust_ecb' 列包含 NA)。
用正确的颜色绘图('trust_ecb' 列不包含 NA)

#Creating dataframe
trust_eur_cyp <- structure(list(year = c(2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 
                    2015L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2009L, 
                    2010L, 2011L, 2012L, 2013L, 2014L, 2015L), group = structure(c(4L, 
                                                                                   4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 
                                                                                   1L, 1L, 1L, 1L), .Label = c("Cypriots", "EU-15", "EU-25", "Greek-Cypriots", 
                                                                                                               "Turkish-Cypriots"), class = "factor"), trust_ecb = c(53.2, 50.2, 
                                                                                                                                                                     44, 33.7, 10.9, 23.2, 19.3, 29.1, 42.1, 36.6, 41.8, 48.1, NA, 
                                                                                                                                                                     NA, 48.8, 48.7, 42.7, 35.1, 17.5, NA, NA)), row.names = c(6L, 
                                                                                                                                                                                                                               7L, 8L, 9L, 10L, 11L, 12L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 
                                                                                                                                                                                                                               36L, 37L, 38L, 39L, 40L, 41L, 42L), class = "data.frame")




#Creating plots
library(plotly)

plot_trust_ecb_cyp <- plot_ly(trust_eur_cyp, y = ~trust_ecb, x= ~year, color = ~group, type = "scatter",
                          mode = "lines+markers",
                          line = list(color = factor(trust_eur_cyp$group, labels = c("FFCF00","9702A7","00A876")), width = 3), 
                          marker = list(color = factor(trust_eur_cyp$group, labels = c("FFCF00","9702A7","00A876")), size = 6),
                          legendgroup = ~group, showlegend = T) %>%
  layout(yaxis = list(title = "%  'Tend to Trust'", titlefont=list(size=20), range = c(0,80), tickfont = list(size = 16)),
     xaxis = list(title = "", dtick = 2, tickfont = list(size = 16)))  

#Recoding 'NA' to '70' to show that now colors in plots are correct
trust_eur_cyp$trust_ecb[is.na(trust_eur_cyp$trust_ecb)] <- 70

#Rerun plotly code above.

标签: rr-plotly

解决方案


我会根据需要为不同组创建一个带有颜色的向量。然后在 中分配colors给它plot_ly。由于存在未使用的因子水平,为了清楚起见,我首先删除了这些并使用了与您相同的 3 种颜色。我相信这适用于您的缺失值。

trust_eur_cyp$group <- droplevels(trust_eur_cyp$group)

pal <- c("#9702A7", "#00A876", "#FFCF00")

plot_trust_ecb_cyp <- plot_ly(trust_eur_cyp, y = ~trust_ecb, x= ~year, color=~group, colors = pal,
                              type = "scatter",
                              mode = "lines+markers",
                              line = list(width = 3), 
                              marker = list(size = 6),
                              legendgroup = ~group, showlegend = T) %>%
  layout(yaxis = list(title = "%  'Tend to Trust'", titlefont=list(size=20), range = c(0,80), tickfont = list(size = 16)),
         xaxis = list(title = "", dtick = 2, tickfont = list(size = 16)))

带有 NA 的 plot_ly


推荐阅读