Reputation: 463
I'm trying to plot two different time series on the same plot using the ggplot2 package in R. I'm not sure why it isn't working. I believe it may be because one of the series is daily data and one is monthly.
I wrote some code for two data frames:
ras <- data.frame(
date=seq(as.Date("2004-10-01"), as.Date("2015-09-02"), by = "day"),
CONSUMERS=runif(3989,80,120)
)
umich <- data.frame(
observation_date=seq(as.Date("2004-10-01"), as.Date("2015-02-01"), by = "month"),
UMCSENT=runif(125,80,100)
)
So I want both of them on the same ggplot. Suppose I try the following:
a <- ggplot() +
geom_line(data=umich,aes(observation_date,UMCSENT)) +
geom_line(data=ras,aes(x=date,y=CONSUMERS))
a
I end up with this error:
Error: Invalid input: time_trans works with objects of class POSIXct only
I thought I could possibly add in argument like this but I get the same error:
a <- ggplot() +
geom_line(data=umich,aes(observation_date,UMCSENT)) +
geom_line(data=ras,aes(x=date,y=CONSUMERS)) +
scale_x_date(format = "%b-%Y")
a
To summarize, I'm trying to plot monthly and daily time series data on the same ggplot
.
Upvotes: 0
Views: 728
Reputation:
Not sure where the problem is. Using your data, here is what I get.
library(ggplot2)
library(scales)
ras <- data.frame(
date=seq(as.Date("2004-10-01"), as.Date("2015-09-02"), by = "day"),
CONSUMERS=runif(3989,80,120)
)
umich <- data.frame(
observation_date=seq(as.Date("2004-10-01"), as.Date("2015-02-01"), by = "month"),
UMCSENT=runif(125,80,100)
)
a <- ggplot() +
geom_line(data=ras,aes(x=date,y=CONSUMERS)) +
geom_line(data=umich,aes(observation_date,UMCSENT), color="red") +
scale_x_date(labels=date_format('%b-%Y'))
a
Upvotes: 1