简体   繁体   中英

Plotting a time series along the diagonal in a plot matrix using `pairs` in R

Suppose I have a data.frame that looks like this

           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
...

I would like to use the pairs function to plot pairs of histograms in order. However, along the diagonal, rather than the names, I would like to have a time series (line graph) for the corresponding column. I am having trouble understanding how to use the diag.panel argument to achieve this. Some example data is given below

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")

You can get there by resetting the usr dimensions of the plot area in the diagonal and then calling lines , as in:

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 
)

This plots each column against the numeric representation of the character date values stored as rownames(toy_example) .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM