首页 > 解决方案 > 从 excel 导入日期以随时间推移作为样本绘制

问题描述

我正在导入一个样本列表(带有日期),并希望将它们绘制为随时间变化的采样频率

从 .csv 导入的数据(事件日期 = d/m/Y)为“col_character()”

我是一个完全的新手 - 我从哪里开始?

标签: rdateplot

解决方案


这是将字符类日期转换为可绘图格式的示例数据。

set.seed(4) # to ensure replicability
dat <- data.frame(date=sprintf("%02d/01/2019", 1:31), value = sample(1:10, 31, replace=TRUE), stringsAsFactors = FALSE)

str(dat)
# 'data.frame': 31 obs. of  2 variables:
#   $ date : chr  "01/01/2019" "02/01/2019" "03/01/2019" "04/01/2019" ...
# $ value: int  1 6 2 4 7 8 6 3 1 1 ...

dat$date <- as.Date(dat$date, format="%d/%m/%Y") #transfor the character format into date format

str(dat)
# 'data.frame': 31 obs. of  2 variables:
#   $ date : Date, format: "2019-01-01" "2019-01-02" "2019-01-03" "2019-01-04" ...
# $ value: int  1 6 2 4 7 8 6 3 1 1 ...

with(dat, plot(date, value, type="l")) #simple line plot

在此处输入图像描述


推荐阅读