0

我需要为我的变量“旅行时间”制作直方图。在其中,我需要绘制回归(相关)数据,即我的观察数据与预测数据。而且我需要在一天和一周的不同时间重复它(简单地说,使用 par 函数制作这样的数字矩阵)。现在,我可以绘制直方图并以矩阵形式排列,但我在内部图中遇到了问题(将 x 和 y 数据与 y=x 线一起绘制,并将它们排列在矩阵中的连续直方图中)。我该怎么做,如下图所示。任何帮助,将不胜感激。谢谢!

在此处输入图像描述

4

1 回答 1

1

一种方法是遍历您的数据并在每次迭代时创建所需的图。这是一个不太完善的示例,但它显示了如何在大图上绘制小图的逻辑。您将不得不调整代码以使其以您需要的方式工作,但这不应该那么困难。

# create some sample dataset (your x values)
a <- c(rnorm(100,0,1))
b <- c(rnorm(100,2,1))
# create their "y" values counterparts
x <- a + 3
y <- b + 4
# bind the data into two dataframes (explanatory variables in one, explained in the other)
data1 <- cbind(a,b)
data2 <- cbind(x,y)

# set dimensions of the plot matrix
par(mfrow = c(2,1))
# for each of the explanatory - explained pair
for (i in 1:ncol(data2))
{
        # set positioning of the histogram
        par("plt" = c(0.1,0.95,0.15,0.9))
        # plot the histogram
        hist(data1[, i])
        # set positioning of the small plot
        par("plt" = c(0.7, 0.95, 0.7, 0.95))
        # plot the small plot over the histogram
        par(new = TRUE)
        plot(data1[, i], data2[, i])
        # add some line into the small plot
        lines(data1[, i], data1[, i])
}
于 2017-01-29T15:13:39.683 回答