0

我有一个数据库df,其中包含 2020 年 1 月至 2020 年 7 月期间世界上某些国家/地区的 Covid 影响信息。按照https://www.youtube.com/watch?v=RrtqBYLf404中的代码,我创建了以下动态地图。

library(plotly)
graph = plot_geo(df, 
                 locationmode = "country-names",
                 frame = ~month) %>%
  add_trace(locations = ~iso,
            z = ~effect_covid_avg,
            zmin = min(df$effect_covid_avg),
            zmax = max(df$effect_covid_avg),
            color = ~effect_covid_avg,
            colorscale = 'Hot', #colorscale = 'heat', 'diverging',  #colorscale = 'YlOrRd',
            text = ~hover,
            hoverinfo = 'text')

graph

但是,我想截取几个月的屏幕截图,以便将它们包含在静态 pdf 文件中。我想进行一些修改以便拥有:

  • 调色板的颜色(蓝色为正,红色为负,白色为 0)。正如你所看到的,我尝试了几个调色板,但都没有成功('heat'、'diverging'、'YlOrRd')。
  • 在图中包含颜色图例。

我的一张截图看起来像这样。 新冠病毒效应

有什么线索吗?

谢谢

4

1 回答 1

0
  1. By modifying the zmin and zmax to the same value with different sign you locate 0 in the middle.
  2. By changing "colorscale" to "colors" you can freely use the palettes. Therefore by applying "RdBu" you have the negatives in red and the positives in blue.

1 and 2 combined look like this:

graph = plot_geo(some_data_values, 
                 locationmode = "country-names",
                 frame = ~month) %>%
  add_trace(locations = ~iso,
            z = ~effect_covid_avg,
            zmin = -0.8,    # below the min (so 0 is at the center of the palette)
            zmax = 0.8,     # over the max (so 0 is at the center of the palette)
            color = ~effect_covid_avg,
            colors = 'RdBu', 
            text = ~hover,
            hoverinfo = 'text')
graph   

Still have to solve how to put the legend in the plot?

于 2021-04-08T14:22:40.363 回答