英文:
How to generate a time series vector(2001/01, 2001/02,.....)?
问题
I can help you with the translation. Here's the translated text:
我有一组包括240行数据的数据(温度数据从2001年01月到2020年12月)。
我想要生成一个类似以下的向量:
c(2001/01, 2001/02, ...... 2001/12, 2002/01, ...... 2020/12)
以便绘制时间序列图。
library(tidyverse)
df <- tibble(tem = runif(240, 1, 10),
time = 1:240)
ggplot(df, aes(time, tem)) +
geom_line() +
geom_point() +
theme_bw()
我该如何在R中实现这个目标?
英文:
I have a 240 rows data(temperature from 2001.01 to 2020.12).
I want to generate a vector just like
c(2001/01, 2001/02,...2001/12,2002/01,......2020/12)
to plot a time series figure.
library(tidyverse)
df <- tibble(tem= runif(240,1,10),
time=1:240)
ggplot(df,aes(time,tem))+
geom_line()+
geom_point()+
theme_bw()
How can I do it in R?
答案1
得分: 1
使用 lubridate
中的 ym
。请注意,所需格式的输出是一个字符串。日期在内部始终以标准格式(数值格式)存储,例如 "2023-01-01 12:12:12",对应于 %Y-%m-%d %H:%M:%S
。
library(lubridate)
begin <- "2001/01"
end <- "2001/04"
format(seq.Date(ym(begin), ym(end), "month"), "%Y/%m")
[1] "2001/01" "2001/02" "2001/03" "2001/04"
如果你想要一个 ts
类型的时间序列对象:
begin <- c(2001, 1)
end <- c(2001, 4)
ts(1:4, start = begin, end = end, frequency = 12)
Jan Feb Mar Apr
2001 1 2 3 4
编辑:
编辑显著更改了答案(几乎相当于不同的答案),仍然在此处添加它。基本上只需添加一个日期对象,然后使用 ggplot2
中的 scale_x_date
进行格式化。
library(dplyr)
library(lubridate)
library(ggplot2)
set.seed(42)
begin <- "2001/01"
end <- "2001/10"
df <- tibble(tem = runif(10, 1, 10),
time = seq.Date(ym(begin), ym(end), "month"))
ggplot(df, aes(time, tem)) +
geom_line() +
geom_point() +
theme_bw() +
scale_x_date(date_labels = "%Y/%m", date_breaks = "1 month")
英文:
Using ym
from lubridate
. Note that the output in your desired format is a string. date
s are internally always stored in a standardized format (numerical), e.g. "2023-01-01 12:12:12", corresponding to %Y-%m-%d %H:%M:%S
library(lubridate)
begin <- "2001/01"
end <- "2001/04"
format(seq.Date(ym(begin), ym(end), "month"), "%Y/%m")
[1] "2001/01" "2001/02" "2001/03" "2001/04"
If you want a time series object of class ts
begin <- c(2001, 1)
end <- c(2001, 4)
ts(1:4, start = begin, end = end, frequency = 12)
Jan Feb Mar Apr
2001 1 2 3 4
EDIT:
The edit changes the answer significantly (almost worthy a different answer), still add it here. Basically just add a date object and do the format in ggplot2
with scale_x_date
.
library(dplyr)
library(lubridate)
library(ggplot2)
set.seed(42)
begin <- "2001/01"
end <- "2001/10"
df <- tibble(tem = runif(10, 1, 10),
time = seq.Date(ym(begin), ym(end), "month"))
ggplot(df, aes(time, tem)) +
geom_line() +
geom_point() +
theme_bw() +
scale_x_date(date_labels = "%Y/%m", date_breaks = "1 month")
答案2
得分: 0
Sure, here is the translated code part without any other content:
Well, just `plot` it maybe.
plot(df$tmp, type='o', pch=20, xaxt='n', xlab='date', ylab='temperature', main='My Header')
axis(1, axTicks(1)[-1], df$date[axTicks(1)])
Data:
v <- seq(as.Date("2001-01-01"), as.Date("2020-12-01"), "month")
set seed(42)
df <- data.frame(date=strftime(v, "%Y.%m"), tmp=rnorm(length(v))
英文:
Well, just plot
it maybe.
plot(df$tmp, type='o', pch=20, xaxt='n', xlab='date', ylab='temperature', main='My Header')
axis(1, axTicks(1)[-1], df$date[axTicks(1)])
Data:
v <- seq(as.Date("2001-01-01"), as.Date("2020-12-01"), "month")
set.seed(42)
df <- data.frame(date=strftime(v, "%Y.%m"), tmp=rnorm(length(v)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论