Reputation: 27
I created a dataset with these variables. But Date variable should be the first 128 day of the year. My problems are:
Can you help me please.
a <- 1
b <- 3
c <- 0
Date <- c(1:128)
Wheat <- rnorm(128, mean = a, sd = b)
Hazelnut <- runif(128, a ,b)
Corn <- rnorm(128, mean = c, sd = b)
df <- as.data.frame(cbind(Date, Wheat, Hazelnut, Corn))
df$Date <- as.Date(df$Date)
Upvotes: 1
Views: 898
Reputation: 22072
How about this:
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(tidyr)
a <- 1
b <- 3
c <- 0
Date <- seq(ymd("2022-01-01"), ymd("2022-01-01")+127, by=1)
Wheat <- rnorm(128, mean = a, sd = b)
Hazelnut <- runif(128, a ,b)
Corn <- rnorm(128, mean = c, sd = b)
df <- data.frame(Date=Date,
Wheat=Wheat,
Hazelnut=Hazelnut,
Corn=Corn)
df <- df %>%
pivot_longer(c("Wheat", "Hazelnut", "Corn"),
names_to="crop",
values_to="vals")
ggplot(df, aes(x=Date, y=vals)) +
geom_line() +
facet_wrap(~crop, ncol=1) +
theme_classic()
df %>% filter(Date <= ymd("2022-02-28")) %>%
ggplot(aes(x=Date, y=vals)) +
geom_line() +
facet_wrap(~crop, ncol=1) +
theme_classic()
Created on 2022-04-19 by the reprex package (v2.0.1)
To make them in a single plot, use:
ggplot(df, aes(x=Date, y=vals, color=crop)) +
geom_line() +
theme_classic()
Upvotes: 1
Reputation: 73842
First, better use data.frame()
to create your data frame to not unnecessarily pollute your workspace with variables you only need once.
For the date you want an origin=
which starts at the day before the time series start in this case.
a <- 1; b <- 3; c <- 0; t1 <- 128
set.seed(42) ## for sake of reproducibility
## always use seed when stochastic processes are involved
dat <- data.frame(Date=as.Date(c(1:t1), origin='2021-12-31'),
Wheat=rnorm(t1, mean=a, sd=b),
Hazelnut=runif(t1, a, b),
Corn=rnorm(t1, mean=c, sd=b))
Looks like this.
head(dat, 3)
# Date Wheat Hazelnut Corn
# 1 2022-01-01 -0.4537862 1.780596 -3.386932
# 2 2022-01-02 1.5673864 1.140588 -0.263794
# 3 2022-01-03 1.1530190 2.415013 6.725711
Then you may use matplot
without the date column, which we need in second step to create axis
and mtext
. Finally we can add a nice legend
.
matplot(dat[-1], type='l', ylab='foo', col=2:4, lty=1, xaxt='n', main='This year')
subs <- !duplicated(substr(dat$Date, 6, 7))
axis(1, seq_along(dat$Date)[subs], labels=F)
mtext(months(dat$Date)[subs], 1, 1, at=seq_along(dat$Date)[subs], cex=.8)
legend('top', legend=names(dat)[-1], lty=1, col=2:4, horiz=TRUE, bty='n', cex=.8)
To create the plot only for two months, just subset the data in the code above, e.g.
matplot(dat[dat$Date < as.Date('2022-03-01'), -1], ...)
In case you want to create an actual time-series object, use ts
,
(df_ts <- ts(with(df, data.frame(Wheat, Hazelnut, Corn)), frequency=365,
start=2022))
# Time Series:
# Start = c(2022, 1)
# End = c(2022, 128)
# Frequency = 365
# Wheat Hazelnut Corn
# 2022.000 5.11287534 1.220670 0.75783511
# 2022.003 -0.69409451 2.079597 -3.88200740
# 2022.005 2.08938523 2.142468 -2.87751133
# 2022.008 2.89858781 2.237903 3.25732456
# [...]
# 2022.340 0.99453216 2.162396 -2.35466343
# 2022.342 -0.28477664 1.940185 0.49024896
# 2022.345 -0.84101482 1.730028 -3.71014271
# 2022.348 -5.07403354 1.560249 3.13762133
where
class(df_ts)
# [1] "mts" "ts" "matrix"
and plot
it.
plot(df_ts)
Upvotes: 2