r - 创建具有特定数据格式的关联图
问题描述
我正在尝试使用发病率()命令创建发病率图和流行曲线。我有以下数据,我应该使用 data_use (class = Date) 和 new_case (class = int) 作为 plot。我正在使用以下代码:
library(incidence)
i <- incidence(outbreak, interval = 1)
plot(i, border = "white")
但是,我收到一个错误:is.finite(x) 中的错误:当我运行第二行时,没有为类型“list”实现默认方法。有没有办法使用这种数据格式和命令创建关联图?感谢您的帮助!
解决方案
我相信这与incidence
函数期望的参数有关。根据帮助?incidence
,第一个说法dates
是:
日期向量,可以作为类的对象提供:整数、数字、日期、POSIXct、POSIXlt 和字符。(请参阅有关数字和字符格式的说明)
目前尚不完全清楚outbreak
,但可能是包含图像中数据的数据框(列列表)?错误消息可能来自提供数据框而不是日期向量。
如果您确实有一个数据框,例如示例:
date_use total_cases new_case
1 2020-03-01 168 25
2 2020-03-02 187 19
3 2020-03-03 200 13
4 2020-03-04 219 19
5 2020-03-05 252 33
6 2020-03-06 295 43
7 2020-03-07 336 41
8 2020-03-08 383 47
9 2020-03-09 470 87
10 2020-03-10 566 96
11 2020-03-11 698 132
您可以基于date_use
和创建日期向量new_case
:
my_dates <- rep(df$date_use, df$new_case)
table(my_dates)
my_dates
2020-03-01 2020-03-02 2020-03-03 2020-03-04 2020-03-05 2020-03-06 2020-03-07 2020-03-08 2020-03-09
25 19 13 19 33 43 41 47 87
2020-03-10 2020-03-11
96 132
然后您可以使用incidence
如下方式创建关联对象:
i <- incidence(my_dates, interval = 1)
i
<incidence object>
[555 cases from days 2020-03-01 to 2020-03-11]
$counts: matrix with 11 rows and 1 columns
$n: 555 cases in total
$dates: 11 dates marking the left-side of bins
$interval: 1 day
$timespan: 11 days
$cumulative: FALSE
然后绘制:
plot(i, border = "white")