【发布时间】:2022-01-18 23:49:31
【问题描述】:
我正在开展一个 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 会在查看器中以及在新窗口中打开它时呈现一个空白框(甚至不是图例)。这是我使用的代码:
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")
【问题讨论】:
-
请提供您的数据集的可重现示例。
-
你的第一个代码 sn-p 给我:
Warning message: 'choropleth' objects don't have these attributes: 'format'。你到底想对日期做什么?预期的输出是什么?