Reputation: 33
I'd really appreciate any help to create a time series plot in R. I'm a total newbie and my programming knowledge is really limited. I just need to create this one graph and it has to be done in R, not Excel.
I have the following monthly data:
Time HML
200207 6.28
200208 3.44
200209 8.03
...
201412 1.47
I have a really hard time to understand how time variable is defined or how it should be converted.
Now I need to plot it so on the X-axis there are only years visible i.e. 2002, 2003, 2004, (...), 2014.
Here's my code:
plot(HML, type="l", lwd=2, col="red", ylab= "% return")
abline(h = 0, col = "black", lty = "solid")
Any help much appreciated.
Best regards, Martin
Upvotes: 2
Views: 11439
Reputation: 379
Using ggplot2
would give you a lot of options. The autoplot
function accepts and plots zoo
objects.
library(ggplot2)
library(magrittr)
library(zoo)
hml_ts <-
ts(rnorm(150),start=c(2002,7),freq=12)
hml_zoo <-
as.zoo(hml_ts)
gg <-
hml_zoo %>%
autoplot() +
xlim(c(2002, end(hml_zoo)))
gg
Upvotes: 1
Reputation: 270268
Read in the data using read.zoo
and class "yearmon"
(which represents a year and month) for the index. Then plot it using xaxt="n"
to suppress the axis. Finally produce the axis yourself using axis
to force years only:
Lines <- "Time HML
200207 6.28
200208 3.44
200209 8.03
201412 1.47"
library(zoo)
HML <- read.zoo(text = Lines, header = TRUE, FUN = as.yearmon, format = "%Y%m",
colClasses = c("character", NA))
plot(HML, xaxt = "n", type = "o", xlab = "Year")
rng <- range(floor(time(HML)))
axis(1, rng[1]:rng[2])
Continued after image.
Note 1: If you already have the data as a data frame DF
DF <- read.table(text = Lines, header = TRUE)
then you could replace the read.zoo
statement with:
HML <- zoo(DF$HML, as.yearmon(as.character(DF$Time), "%Y%m"))
Note 2: I can't tell whether the actual data has data for every month from the first to the last but the above work even if not.
Upvotes: 1
Reputation: 118
First, create a time series object! (using ts()
)
Then plot, I guess R will show only years on X-axis automatically!
#make Time to time series object
a<-ts(rnorm(150),start=c(2002,7),freq=12);a
plot(a, type="l", lwd=2, col="red", ylab= "% return",xlim=c(2002,2014),axes=F)
axis(1,at=2002:2014,labels=2002:2014);axis(2);box()
Explain code:
ts()
means the times of the first observationxlim
!axes=F
in plot means do not show the default axis. Then, create what axis I hoped by axis()
and label
show all years!BTW You can find more details by typing ?ts
in R !
Upvotes: 2
Reputation: 10421
There are many ways to achieve this... Here is one of them. We'll be using hypothetical data spanning from 2002-01 to 2012-12 (having gaps doesn't matter in this case). Note that we've used the 1st of each month to have a complete date, easily recognizable by the function as.POSIXct
which will convert strings to time objects.
Time <- c("2002-01-01", "2002-02-01", "2003-03-01", "2003-04-01",
"2004-05-01", "2004-06-01", "2005-07-01", "2005-08-01",
"2006-09-01", "2006-10-01", "2006-11-01", "2008-01-01",
"2008-02-01", "2009-02-01", "2009-03-01", "2010-04-01",
"2010-05-01", "2010-06-01", "2011-07-01", "2011-08-01",
"2012-09-01", "2012-10-01", "2012-11-01", "2012-12-01")
HML <- rnorm(n = 24, mean = 6, sd = 2)
dat <- data.frame(Time=as.POSIXct(Time), HML)
rm(Time, HML)
And then use the plot
function:
plot(HML ~ Time, type="l", col="red", lwd=2, ylab= "% return", data=dat)
Upvotes: 1