【问题标题】:How to fix incorrect colors with Plotly when column contains 'NA'当列包含“NA”时如何使用 Plotly 修复不正确的颜色
【发布时间】:2019-11-03 19:10:18
【问题描述】:

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

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

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

Plot 的颜色不正确('trust_ecb' 列包含 NA)。
Plot 的颜色正确('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.

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    我会根据需要为不同组创建一个带有颜色的矢量。然后在plot_ly 中将colors 分配给它。由于存在未使用的因子水平,为了清楚起见,我首先删除了这些并使用了与您相同的 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)))
    

    【讨论】:

    • 太好了,谢谢。你知道为什么会这样吗?两个代码不应该产生相同的图表吗?
    • 老实说我不确定。当我四处寻找线索时,我在 github 上发现了这个:github.com/ropensci/plotly/issues/1126。我想这可能与plot_ly 如何处理NA 并用痕迹分隔组以及未使用的因子水平有关。但我真的不确定整个事情。
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2022-01-13
    • 2019-07-16
    相关资源
    最近更新 更多