Map with time slider using Plotly in R

Hello friends! we’ll be learning to create the map with time slider using plotly in R. Very simple and reusable code in tutorial.

Data

Data is as shown below with country, covid cases and date fields.

countrynew_cases_per_millionstart_of_weekcode
Afghanistan0.02142857124-02-2020AFG
Afghanistan0.15014285702-03-2020AFG
Afghanistan0.36609-03-2020AFG
Afghanistan1.29816-03-2020AFG
Afghanistan3.57914285723-03-2020AFG
Afghanistan5.82042857130-03-2020AFG
Afghanistan8.44185714306-04-2020AFG
Afghanistan9.99457142913-04-2020AFG
Afghanistan20.3982857120-04-2020AFG

Code

Commented code as following with the data

#load library
library(dplyr)
library(plotly)
library(htmlwidgets)
#load data
df <- read.csv("graph.csv")
#create map
p <- plot_geo(df, locationmode = 'world') %>%
add_trace( z = ~df$new_cases_per_million, locations = df$code, frame=~df$start_of_week,
color = ~df$new_cases_per_million)
#export as html file
htmlwidgets::saveWidget(p, file = "map.html")

Get the code here

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries


Leave a Reply

Your email address will not be published. Required fields are marked *