在 R 中使用日期格式时绘制地图不呈现
Plotly map not rendering when using date format in R
我正在从事一个 Covid EDA 实践项目,我正在尝试创建一个显示案例随时间变化的地图。
Our World in Data提供的数据中,日期是一个字符串。如果我将日期保留为字符串,Plotly 会正确呈现地图,但日期是乱序的。我使用了以下代码:
cot_map <- country %>%
select(location, date, total_cases)
plot_geo(cot_map,
locationmode = "country names",
format = ~date) %>%
add_trace(locations = ~location,
z = ~total_cases,
zmin = 0,
zmax = 51000000,
color = ~total_cases)
如果我将日期强制转换为日期类型,它确实会在数据框中变成日期,但是 Plotly 会在查看器中以及如果我在新的 window。这是我使用的代码:
cot_map <- country %>%
select(location, date, total_cases)
cot_map$date <- as.Date(cot_map$date, format = "%d/%m/%Y")
plot_geo(cot_map,
locationmode = "country names",
format = ~date) %>%
add_trace(locations = ~location,
z = ~total_cases,
zmin = 0,
zmax = 51000000,
color = ~total_cases)
我也尝试使用基础 R 和 lubridate
包转换为日期和 POSIX,但没有任何效果。我已经在网上搜索但未能找到解决方案。我使用的是最新版本的 RStudio Desktop。有人遇到过这个吗?
编辑
这是我使用的数据框的一小部分,其中包含对大多数国家/地区的大约 13 万个观察结果。
iso_code location date total_cases
1 AFG Afghanistan 24/02/2020 5
2 AFG Afghanistan 25/02/2020 5
3 AFG Afghanistan 26/02/2020 5
4 AFG Afghanistan 27/02/2020 5
5 AFG Afghanistan 28/02/2020 5
6 AFG Afghanistan 29/02/2020 5
structure(list(iso_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG"), location = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan"), date = c("24/02/2020",
"25/02/2020", "26/02/2020", "27/02/2020", "28/02/2020", "29/02/2020"
), total_cases = c(5, 5, 5, 5, 5, 5)), row.names = c(NA, 6L), class = "data.frame")
我不太确定这是怎么回事,因为 'choropleth' 对象没有代码建议的 format
属性。我猜你想要一个动画。
要为 frame
属性提供日期,您需要将它们设为 factor
:
library(plotly)
library(dplyr)
country <- structure(list(iso_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG"), location = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan"), date = c("24/02/2020",
"25/02/2020", "26/02/2020", "27/02/2020", "28/02/2020", "29/02/2020" ),
total_cases = seq(0, 51000000, length.out = 6)), row.names = c(NA, 6L), class =
"data.frame")
cot_map <- country %>% select(location, date, total_cases)
cot_map$date <- as.factor(as.Date(cot_map$date, format = "%d/%m/%Y"))
plot_geo(cot_map,
locationmode = "country names",
frame = ~date) %>%
add_trace(locations = ~location,
z = ~total_cases,
zmin = 0,
zmax = 51000000,
color = ~total_cases) %>%
animation_slider(
currentvalue = list(prefix = "Date: ")
)
PS:我会用 max(country$total_cases)
来定义 zmax
。
我正在从事一个 Covid EDA 实践项目,我正在尝试创建一个显示案例随时间变化的地图。 Our World in Data提供的数据中,日期是一个字符串。如果我将日期保留为字符串,Plotly 会正确呈现地图,但日期是乱序的。我使用了以下代码:
cot_map <- country %>%
select(location, date, total_cases)
plot_geo(cot_map,
locationmode = "country names",
format = ~date) %>%
add_trace(locations = ~location,
z = ~total_cases,
zmin = 0,
zmax = 51000000,
color = ~total_cases)
如果我将日期强制转换为日期类型,它确实会在数据框中变成日期,但是 Plotly 会在查看器中以及如果我在新的 window。这是我使用的代码:
cot_map <- country %>%
select(location, date, total_cases)
cot_map$date <- as.Date(cot_map$date, format = "%d/%m/%Y")
plot_geo(cot_map,
locationmode = "country names",
format = ~date) %>%
add_trace(locations = ~location,
z = ~total_cases,
zmin = 0,
zmax = 51000000,
color = ~total_cases)
我也尝试使用基础 R 和 lubridate
包转换为日期和 POSIX,但没有任何效果。我已经在网上搜索但未能找到解决方案。我使用的是最新版本的 RStudio Desktop。有人遇到过这个吗?
编辑
这是我使用的数据框的一小部分,其中包含对大多数国家/地区的大约 13 万个观察结果。
iso_code location date total_cases
1 AFG Afghanistan 24/02/2020 5
2 AFG Afghanistan 25/02/2020 5
3 AFG Afghanistan 26/02/2020 5
4 AFG Afghanistan 27/02/2020 5
5 AFG Afghanistan 28/02/2020 5
6 AFG Afghanistan 29/02/2020 5
structure(list(iso_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG"), location = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan"), date = c("24/02/2020",
"25/02/2020", "26/02/2020", "27/02/2020", "28/02/2020", "29/02/2020"
), total_cases = c(5, 5, 5, 5, 5, 5)), row.names = c(NA, 6L), class = "data.frame")
我不太确定这是怎么回事,因为 'choropleth' 对象没有代码建议的 format
属性。我猜你想要一个动画。
要为 frame
属性提供日期,您需要将它们设为 factor
:
library(plotly)
library(dplyr)
country <- structure(list(iso_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG"), location = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan"), date = c("24/02/2020",
"25/02/2020", "26/02/2020", "27/02/2020", "28/02/2020", "29/02/2020" ),
total_cases = seq(0, 51000000, length.out = 6)), row.names = c(NA, 6L), class =
"data.frame")
cot_map <- country %>% select(location, date, total_cases)
cot_map$date <- as.factor(as.Date(cot_map$date, format = "%d/%m/%Y"))
plot_geo(cot_map,
locationmode = "country names",
frame = ~date) %>%
add_trace(locations = ~location,
z = ~total_cases,
zmin = 0,
zmax = 51000000,
color = ~total_cases) %>%
animation_slider(
currentvalue = list(prefix = "Date: ")
)
PS:我会用 max(country$total_cases)
来定义 zmax
。