1

假设我有 2 个数据集,1 个在 x 轴上有 50 天,一个有 20 天,使用代码创建

par(mfrow=c(2,1))
data1=rnorm(50)
plot(data1,type='l',xlab='Days')
data2=rnorm(20)
plot(data2,type='l',xlim=c(0,50),xlab='Days')

在此处输入图像描述

实际上,数据集 2 中的第 1 天对应于数据集 1 中的第 20 天,所以我想绘制数据集 2,使得 x 轴,如上,从 0 到 50,但数据从 x=20 绘制到 x = 40. 我猜有一个简单的答案,但我在网上找不到...

谢谢你。

4

1 回答 1

3

传球xy论据进行绘图。

plot(20:39,data2,type='l',xlim=c(0,50),xlab='Days')

虽然如果你用相同的轴绘制多个数据集,你最好使用 ggplot2 或 lattice。你的情节会看起来好多了。

all_data <- data.frame(
  day = c(1:50, 20:39),
  y   = c(runif(50), rnorm(20)),
  grp = factor(rep(1:2, times = c(50, 20)))  
)

#ggplot2 style
library(ggplot2)
ggplot(all_data, aes(day, y)) +
  geom_line() +
  facet_grid(grp ~ .)

#lattice style
library(lattice)
xyplot(y ~ day | grp, all_data, type = "l", layout = c(1, 2))
于 2012-07-03T10:49:54.363 回答