将数据框架转换为用于绘图的时间序列

发布于 2025-02-10 18:42:05 字数 417 浏览 3 评论 0原文

我有数据框架:

 year   sum1
   <chr> <dbl>
 1 1979      4
 2 1980     14
 3 1981     14
 4 1982     13
 5 1983      6
 6 1984     15
 7 1985     15
 8 1986      7
 9 1987     10
10 1988     12

我想将其转换为时间序列以制作图。有什么想法吗?

I have the dataframe:

 year   sum1
   <chr> <dbl>
 1 1979      4
 2 1980     14
 3 1981     14
 4 1982     13
 5 1983      6
 6 1984     15
 7 1985     15
 8 1986      7
 9 1987     10
10 1988     12

and I would like to convert it to time series in order to make a plot. Any idea?

foo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抠脚大汉 2025-02-17 18:42:05

只需将年份转换为数字而不是字符,

DF2 <- transform(DF, year = as.numeric(year))
plot(DF2, type = "l", lwd = 2, lty = 2, col = "blue", 
  xlab = "year", ylab = "value", main = "my title")

如果您想将其转换为时间序列,则

library(zoo)

z <- read.zoo(DF, FUN = as.numeric)

plot(z, type = "l", lwd = 2, lty = 2, col = "blue", 
  xlab = "year", ylab = "value", main = "my title")

library(ggplot2)
autoplot(z, linetype = I("dashed"), lwd = I(1.5), col = I("blue")) + 
  xlab("year") + 
  ylab("value") + 
  ggtitle("my title")

然后将其转换为动物园系列,并使用经典图形或GGPLOT2(AutoPlot)。

注意

Lines <- "year   sum1
 1 1979      4
 2 1980     14
 3 1981     14
 4 1982     13
 5 1983      6
 6 1984     15
 7 1985     15
 8 1986      7
 9 1987     10
10 1988     12"
DF <- read.table(text = Lines, colClasses = c(year = "character"))

You only need to convert the year to numeric rather than character

DF2 <- transform(DF, year = as.numeric(year))
plot(DF2, type = "l", lwd = 2, lty = 2, col = "blue", 
  xlab = "year", ylab = "value", main = "my title")

If you do want to convert it to a time series then convert it to a zoo series and use classic graphics or ggplot2 (autoplot).

library(zoo)

z <- read.zoo(DF, FUN = as.numeric)

plot(z, type = "l", lwd = 2, lty = 2, col = "blue", 
  xlab = "year", ylab = "value", main = "my title")

library(ggplot2)
autoplot(z, linetype = I("dashed"), lwd = I(1.5), col = I("blue")) + 
  xlab("year") + 
  ylab("value") + 
  ggtitle("my title")

screenshot

Note

Lines <- "year   sum1
 1 1979      4
 2 1980     14
 3 1981     14
 4 1982     13
 5 1983      6
 6 1984     15
 7 1985     15
 8 1986      7
 9 1987     10
10 1988     12"
DF <- read.table(text = Lines, colClasses = c(year = "character"))
江湖正好 2025-02-17 18:42:05

转换as.xts绘图。首先通过1月1日完成日期

my_ts <- xts::as.xts(dat$sum1, order.by=as.Date(paste0(dat$year, '-01-01')))
plot(my_ts)

。 。


dat <- structure(list(year = 1979:1988, sum1 = c(4L, 14L, 14L, 13L, 
6L, 15L, 15L, 7L, 10L, 12L)), class = "data.frame", row.names = 1979:1988)

Convert as.xts and plot. First complete the dates with January 1.

my_ts <- xts::as.xts(dat$sum1, order.by=as.Date(paste0(dat$year, '-01-01')))
plot(my_ts)

enter image description here


Data:

dat <- structure(list(year = 1979:1988, sum1 = c(4L, 14L, 14L, 13L, 
6L, 15L, 15L, 7L, 10L, 12L)), class = "data.frame", row.names = 1979:1988)
梅窗月明清似水 2025-02-17 18:42:05

我们可以使用tsibble软件包将数据框架转换为times系列OBEJCT:

library(tsibble)
library(fable)
df %>% 
  as_tsibble(index = year) %>% 
  autoplot(vars(sum1))

“在此处输入图像说明”

We could use tsibble package to transfrom dataframe to times series obejct:

library(tsibble)
library(fable)
df %>% 
  as_tsibble(index = year) %>% 
  autoplot(vars(sum1))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文