我想在同一张图上绘制两个系列。不幸的是,这个系列并不是在同一天开始的。
> 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
有没有办法绘制这两个系列,以便观察与适当的日期相匹配?
以下代码目前生成:
http://i.imgur.com/msXCaAU.jpg
以下是我使用的代码:
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')
答案 0 :(得分:2)
使用ggplot2软件包的简单解决方案(请记住,解决方案基本上是一行的。大多数代码都是重建丢失数据框所必需的):
df <- data.frame(seq(c(ISOdate(2013,3,20)), by = "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)), by = "day", length.out = 10),sample.int(20,10,replace=TRUE))
names(df1) <- c("date","value")
df1$desc <- rep("subs",10)
按行划分2个数据框并绘制
require(ggplot2)
dfcombi <- rbind(df,df1)
ggplot(dfcombi,aes(date,value,colour=desc)) + geom_line()
答案 1 :(得分:0)
您可以将两者连接在一起然后使用晶格。我无法测试我的代码,因为我没有您的数据,但是
library(lattice)
colnames(spend_df) <- c("date", "val")
spend_df$set <- "spend"
colnames(subs_df) <- c("date", "val")
subs_df$set <- "subs"
df <- rbind(spend_df, subs_df)
xyplot(val ~ date, data=df, groups=set, auto.key=TRUE)