【发布时间】:2020-04-01 16:56:10
【问题描述】:
我正在尝试使用以下 R 代码绘制冠状病毒在印度与世界其他地区的传播情况 我将 corona_world 作为数据框并从中创建了 daily_confirmed 数据框。我已经消除了变量列 'India' 中的所有 NA 值。但仍然错误说它没有相同的长度。我不明白为什么它不起作用。请帮忙
错误:Tibble 列必须具有一致的长度,只有长度为 1 的值被回收:* 长度 61:列 y * 长度 10358:列 x
daily_confirmed <- corona_world %>%
dplyr::select(Confirmed) %>%
dplyr::mutate(country = dplyr::if_else(corona_world$Country.Region == "India",
"India",
"Rest of the World")) %>%
dplyr::group_by(corona_world$ObservationDate, country) %>%
dplyr::summarise(total = sum(Confirmed, rm.na=TRUE)) %>%
dplyr::ungroup() %>%
tidyr::pivot_wider(names_from = country, values_from = total)
daily_confirmed <- daily_confirmed[-c(1:8),]
daily_confirmed %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~ corona_world$ObservationDate,
y = ~ India,
type = "scatter",
mode = "lines+markers",
name = "India") %>%
plotly::add_trace(x = ~ corona_world$ObservationDate,
y = ~ Rest of the World,
type = "scatter",
mode = "lines+markers",
name = "Rest of the World") %>%
plotly::layout(title = "",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of New Cases"),
xaxis = list(title = "Date"),
hovermode = "compare",
margin = list(b = 10,
t = 10,
pad = 2
))
【问题讨论】: