使用R中的“pair”在绘图矩阵中绘制沿对角线的时间序列

时间:2014-09-09 23:13:51

标签: r plot data-visualization scatter-plot

假设我有data.frame看起来像这样

           edmar2014 edmar2015 edmar2016
2014-01-02    99.725    99.465    98.585
2014-01-03    99.735    99.440    98.555
2014-01-06    99.735    99.445    98.590
...

我想使用pairs函数按顺序绘制直方图对。但是,沿着对角线,而不是名称,我想有一个相应列的时间序列(线图)。我无法理解如何使用diag.panel参数来实现此目的。下面给出了一些示例数据

toy_example <- structure(list(edmar2014 = c(99.725, 99.735, 99.735, 99.735, 
99.735, 99.735, 99.74, 99.745, 99.75, 99.75, 99.745, 99.75, 99.75, 
99.75, 99.745, 99.74, 99.745, 99.745, 99.745, 99.745), edmar2015 = c(99.465, 
99.44, 99.445, 99.45, 99.405, 99.395, 99.46, 99.49, 99.46, 99.435, 
99.445, 99.445, 99.46, 99.425, 99.47, 99.48, 99.48, 99.495, 99.515, 
99.52), edmar2016 = c(98.585, 98.555, 98.59, 98.605, 98.465, 
98.465, 98.605, 98.67, 98.595, 98.54, 98.56, 98.56, 98.545, 98.48, 
98.62, 98.665, 98.625, 98.67, 98.71, 98.695)), .Names = c("edmar2014", 
"edmar2015", "edmar2016"), row.names = c("2014-01-02", "2014-01-03", 
"2014-01-06", "2014-01-07", "2014-01-08", "2014-01-09", "2014-01-10", 
"2014-01-13", "2014-01-14", "2014-01-15", "2014-01-16", "2014-01-17", 
"2014-01-21", "2014-01-22", "2014-01-23", "2014-01-24", "2014-01-27", 
"2014-01-28", "2014-01-29", "2014-01-30"), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

您可以通过重置对角线中绘图区域的usr尺寸然后调用lines来实现目标,如:

pairs(
  toy_example,
  diag.panel=function(x) {
    dates <- as.Date(rownames(toy_example))
    par(usr=c(range(dates),range(x)+c(-0.1,0.1)))
    lines(dates,x)
  },
  text.panel=NULL 
)

这会将每列绘制为存储为rownames(toy_example)的字符日期值的数字表示。

enter image description here