Reputation: 1351
I have the following Dataset (Bu.Dis):
row.names Date DIS
116 1Q1 0.1120
117 1Q2 0.1104
118 1Q3 0.3794
119 1Q4 0.3983
120 2Q1 0.3175
121 2Q2 0.2275
122 2Q3 0.2171
123 2Q4 0.1973
124 3Q1 0.2499
125 3Q2 0.1819
126 3Q3 0.2613
127 3Q4 0.2302
128 4Q1 0.3795
129 4Q2 0.2406
130 4Q3 0.2486
131 4Q4 0.2464
Using ggplot to plot it as a time series is pretty easy:
require(ggplot2)
Bu.Dis["Date"] <- NA
Bu.Dis$Date <- seq(as.Date("2001/1/1"), as.Date("2004/12/31"), by = "3 months")
ggplot(Bu.Dis, aes(Date, DIS))+geom_line()+
theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"),
panel.grid.major = element_line(colour = "grey", linetype = "dotted"))
However, when i try to use the changepoint package, I have to transform the dataframe to a timeseries vector.
Bu.Dis.ts <- ts(Bush.Dis[,2], c(2001, 1), frequency = 4)
var.Bu.Dis=cpt.var(Bu.Dis.ts, method="PELT")
plot(var.Bu.Dis)
Plotting this vector with "plot" is also pretty easy, but I want to do it with ggplot. The problem is, that var.Bu.Dis is a S4-object. How do I plot this S4 object with ggplot?
By the way, is there an easier way to transform my entries in "Date" into a timeseries compatible date-format (preferable into a quarter-format like 2001-1 or 2001-Q1)?
Upvotes: 2
Views: 1504
Reputation: 6363
I would look to ggfortify, which can parse ts objects into ggplot2: http://rpubs.com/sinhrks/plot_ts
Another nice thing about ggfortify is that it support timeseries statistics: http://rpubs.com/sinhrks/plot_tsstats
To format the date field as year and quarter, try the following:
library(zoo)
yq <- as.yearqtr(Bu.Dis$Date, format = "%Y/%m/%d")
format(yq, format = "%Y-0%q")
In combination, this should give you what you need. Assuming you have multiple observations per quarter, you may need to do some aggregation with stat_summary().
Cheers,
Adam
Upvotes: 1
Reputation: 1351
I think I answered this question on my own (at least most the problems I had): ggplot cannot plot ts data. So you have to transform it.
ggplot(melt(data.frame(time=as.numeric(time([email protected])), [email protected]), id.vars="time"), aes(time, value)) +
geom_line() +
geom_vline(xintercept = cpts.ts(var.Bu.Dis), colour="red", linetype = "dotdash")
geom_vline is used to plot the "change point".
But I still get this message: "Don't know how to automatically pick scale for object of type ts. Defaulting to continuous"
But there is still one problem left: In the x-axis, only the years are shown, but not the quarters. How can I change that?
Upvotes: 1