【问题标题】:Plotly animated map not showing countries with NA valuesPlotly 动画地图未显示具有 NA 值的国家/地区
【发布时间】:2020-08-30 17:10:12
【问题描述】:

我在plotly community forum 上发布了这个,但绝对没有任何活动!希望您能在这里提供帮助:

我有地图时间序列数据,有些国家没有数据,plotly 根本没有绘制它们。我可以对它们进行概述,它们看起来不同,但那里似乎没有数据丢失(即我想要一个图例条目)。我怎样才能做到这一点?这是一个代表:

library(plotly)
library(dplyr)

data = read.csv('https://github.com/lc5415/COVID19/raw/master/data.csv')


l <- list(color = toRGB("grey"), width = 0.5)

g <- list(
  scope = 'world',
  countrycolor = toRGB('grey'),
  showframe = T,
  showcoastlines = TRUE,
  projection = list(type = 'natural earth')
)

map.time = data %>%
  plot_geo() %>% 
  add_trace(z = ~Confirmed, color = ~Confirmed, frame = ~Date, colors = 'Blues',
            text = ~Country, locations = ~Alpha.3.code, marker = list(line = l)) %>% 
  colorbar(title = 'Confirmed') %>%
  layout(
    title = 'Number of confirmed cases over time',
    geo = g
  ) %>% 
  animation_opts(redraw = F) %>%
  animation_slider(
    currentvalue = list(
      prefix = paste0("Days from ",
                      format(StartDate, "%B %dnd"),": "))) %>%
  plotly_build()

map.time

请注意,缺少数据的国家(例如俄罗斯)的数据点与所有其他国家一样多,问题不在于它们没有出现在传递给 plotly 的 dtaframe 中。

【问题讨论】:

    标签: r maps plotly


    【解决方案1】:

    处理此问题的明显方法是为工具提示创建一个单独的标签列,该列读取 NA 值的“无数据”(否则为实际值),然后将实际 NA 值设为 0。这将提供统一的外观到所有国家,但在某个国家/地区没有数据时正确地告诉您。

    map.time = data %>%
      mutate_if(is.numeric, function(x) {x[is.na(x)] <- -1; x}) %>%
      plot_geo() %>% 
      add_trace(z = ~Confirmed, color = ~Confirmed, frame = ~Date, colors = 'Blues',
                text = ~Country, locations = ~Alpha.3.code, 
                marker = list(line = l)) %>% 
      colorbar(title = 'Confirmed') %>%
      layout(
        title = 'Number of confirmed cases over time',
        geo = g
      ) %>% 
      animation_opts(redraw = F) %>%
      animation_slider(
        currentvalue = list(
          prefix = paste0("Days from ",
                          format(StartDate, "%B %dnd"),": "))) %>%
      plotly_build()
    

    这给出了:

    【讨论】:

    • 嗨,Allan,感谢您的快速回复,理想情况下,我希望有一个单独的图例框,上面写着 NA,以明确区分有和没有缺失值的国家/地区。在我看来,使用 -1 而不是 NA 是不理想的,因为这些国家/地区的颜色会与低值国家/地区的颜色混合。
    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多