因此,我通过构建自己的专用散点图GEOM来扩展ggplot2。假设映射到x美感的输入列的范围是0到1000。如果我想给我的GEOM用户一个选项来设置要显示的x轴范围,那么我该如何做呢?必须对数据进行子集化?
StatTimeLine <- ggproto("StatTimeLine", Stat,
required_aes = c("x"),
default_aes = aes(),
compute_group = function(data, scales, xmin, xmax) {
if (substr(xmin,1,1)=="-")
xmin <- as.Date(-1*as.numeric(difftime(substr(xmin, 2, nchar(xmin)), "0000-01-01")), origin="0000-01-01")
else
xmin <- as.Date(xmin, origin="0000-01-01")
if (substr(xmax,1,1)=="-")
xmax <- as.Date(-1*as.numeric(difftime(substr(xmax, 2, nchar(xmax)), "0000-01-01")), origin="0000-01-01")
else
xmax <- as.Date(xmax, origin="0000-01-01")
data <- data[data$x >= xmin & data$x <= xmax, ]
data
})