在ggplot2中用矩形/阴影区域突出显示值

时间:2019-02-13 21:25:37

标签: r ggplot2 time-series

大家好,我有以下示例数据:

'use strict';

function splitFileNameFromPath(path, slashType) {
  const pathArray = path.split(slashType),
        fileName = pathArray.pop(),
        dirPath = pathArray.join(slashType);

  return [dirPath, fileName];
}

const path = "C:\\Documents\\Newsletters\\Summer2018.pdf";
const slash = "\\";

const res = splitFileNameFromPath(path, slash);

console.log(res);
console.log(path === res.join(slash));

然后我生成此图像:

enter image description here

我想做的是用一个正方形或其他东西突出显示一个特定的时间间隔,所以它看起来像这样:

enter image description here

正如我所说,不必一定要是一条实线,也可以是一个阴影区域,有人知道我该怎么做吗?

2 个答案:

答案 0 :(得分:3)

正如@dww所述,geom_rect有效。添加下面的层:

  geom_rect(
    aes(
      xmin = as.Date('1993-06-01'),
      xmax = as.Date('1994-11-01'),
      ymin = min(index),
      ymax = 0
    ),
    fill = NA,
    color = "black",
    size = 2
  )

enter image description here

答案 1 :(得分:2)

这将创建一个打开的框,但是如果需要的话,在末尾添加一个附加点可以将其关闭:

+ geom_path(data=data.frame(y=c(0,-2,-2,0), x=as.Date('1980-01-01')+30*c(40,40,60,60)),
            mapping=aes(x=x,y=y)) 

enter image description here

主要障碍是要认识到x轴已按日期分类。

相关问题