重叠图中的长度不同

时间:2015-10-12 21:25:01

标签: r plot line overlap

我对这个情节的人有问题,也许你可以找到解决方案。

因此,我将9月份和9月份的收入日用红线绘制。我也在十月与Octubre收入日的同一图表中进行密谋。

这就是我想要做的。

enter image description here

这就是我的R ......中发生的事情。

enter image description here

以下是用于将它们一起绘制的代码,但由于它们具有不同的长度,因此绘图重叠确实令人困惑。

plot(as.Date(Septiembre$Date), cumsum(Septiembre$TMM), type="l", col="red" )
par(new=TRUE)
plot(as.Date(Octubre$Date), cumsum(Octubre$TMM), type="l", col="green" )

这是Septiembre和Octubre数据。

> Septiembre$Date
[1] "2015-09-24" "2015-09-26"
> Septiembre$TMM
[1] 720 540
> Octubre$Date
[1] "2015-10-01" "2015-10-03" "2015-10-09" "2015-10-10" "2015-10-11"
> Octubre$TMM
[1] 400 540 360 720 360

1 个答案:

答案 0 :(得分:1)

如果计算x和y范围,则可以设置合适的窗口大小。

## Ranges
xlim <- range(as.Date(Septiembre$Date), as.Date(Octubre$Date))
ylim <- range(0, sapply(list(Septiembre$TMM, Octubre$TMM), cumsum))

## Make the plot
plot(as.Date(Septiembre$Date), cumsum(Septiembre$TMM), type="l", col="red",
     xlim=xlim, ylim=ylim)
points(as.Date(Octubre$Date), cumsum(Octubre$TMM), type="l", col="green" )

我不确定这是否是你要找的范围。这是另一种可能性,其中日期转换为整数,并在每种情况下标准化为从0开始。

## Normalize the date data
dateRanges <- lapply(list(Septiembre$Date, Octubre$Date), function(x) {
    res <- as.integer(as.Date(x))
    res - res[1]
})

xlim <- c(0, max(unlist(dateRanges)))
ylim <- range(0, sapply(list(Septiembre$TMM, Octubre$TMM), cumsum))

## Make the plot
plot(dateRanges[[1]], cumsum(Septiembre$TMM), type="l", col="red",
     xlim=xlim, ylim=ylim, xaxt='n', xlab="Date", ylab="")
par(new=TRUE)
plot(dateRanges[[2]], cumsum(Octubre$TMM), type="l", col="green",
     xlim=xlim, ylim=ylim, axes = FALSE, xlab="", ylab="")

enter image description here