r - Plot two series on same graph that begin on different dates -
i've got 2 series want plot on same graph. unfortunately series not begin on same date.
> head(spend_df) date spend 1 2012-05-24 1754.73 2 2012-05-25 3270.75 3 2012-05-26 3168.60 4 2012-05-27 3963.20 5 2012-05-28 3123.68 > head(subs_df) date subs 1 2013-11-08 820.8462 2 2013-11-09 801.5087 3 2013-11-10 820.0475 4 2013-11-11 842.5435 5 2013-11-12 848.4188 is there way plot these 2 series observations match appropriate dates?
the code below generates:
http://i.imgur.com/msxcaau.jpg

and here code i'm using:
spend_df <- read.table('c:\\users\\...spend.csv',header=true,sep=',') subs_df <- read.table('c:\\users\\...subs.csv', header=true,sep=',') colnames(spend_df) <- c("date", "spend") colnames(subs_df) <- c("date", "subs") interval <- 60 with(spend_df, plot(spend, xlab="", ylab="", type='l', axes=false)) axis(2) axis(1, labels = false, at=seq(1,nrow(spend_df),interval)) boolean_interval <- c(true,c(rep(false,interval-1))) ticks <- spend_df$date[boolean_interval] text(seq(1, length(spend_df$date), interval), par("usr")[3] + 1, srt = 45, adj = 1, labels = ticks, xpd = true) box() par(new=t) plot(subs_df$subs, axes=false, xlab="", ylab="", type='l')
an easy solution using ggplot2 package ( keep in mind solution one-liner. of code necessary reconstruct missing data frames ):
df <- data.frame(seq(c(isodate(2013,3,20)), = "day", length.out = 10),sample.int(20,10,replace=true)) names(df) <- c("date","value") df$desc <- rep("spend",10) df1 <- data.frame(seq(c(isodate(2013,3,25)), = "day", length.out = 10),sample.int(20,10,replace=true)) names(df1) <- c("date","value") df1$desc <- rep("subs",10) bind 2 data frames row , plot
require(ggplot2) dfcombi <- rbind(df,df1) ggplot(dfcombi,aes(date,value,colour=desc)) + geom_line() 
Comments
Post a Comment