如何在R中的xyplot中获取带阴影的背景? [英] How to get shaded background in xyplot in R?

查看:75
本文介绍了如何在R中的xyplot中获取带阴影的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用lattice包中的xyplot,我绘制了多年的时间序列.在某些年份中,我会添加一个阴影区域,以指示该时间段是特殊"时间(例如战争).

using xyplot from the lattice package, I plot a time series over a number of years. I would to add a shaded area for some of these years to indicate that this time period was "special" (e.g. war).

如果这很简单,请道歉,但是我不知道该怎么做,所以如果有人可以帮助我,或者至少指出正确的方向,我将很高兴.我认为我的主要问题是我真的不知道如何解决这个问题.我对R还是特别陌生,尤其是对lattice.

Please apologize if this is trivial, but I could not figure out how to do that, so I would be happy if someone could help me out, or at least point me in the right direction. I think my main problem is that I don't really know how to approach this problem. I am still relatively new to R, and to lattice in particular.

这是一个最小的示例:

 xyplot( rnorm(100) ~ 1:100, type="l", col="black")

在相应的绘图中,我希望从绘图区域的底部到顶部的背景颜色(从x值开始,从45到65)变为浅灰色.

In the corresponding plot, I would like the color of the background (from say x-values of 45 until 65) from the bottom to the top of the plotting area be shaded in, say, light grey.

请注意,到目前为止,我发现的解决方案都使用base图形和polygon函数,但是其目的是为曲线下方或上方的区域着色,这与我想要的方式不同. .我不只是想在线条下方或线条上方阴影区域.相反,我想在给定的时间间隔内对整个背景进行着色.

Note that solutions that I have found so far use base graphics and the polygon-function, but there the intention is to shade the area under or above a curve, which is different from what I would like to do. I don't "just" want to shade the area below my line, or above my line. Instead I would like to shade the entire background for a given time interval.

如果有人可以在这里帮助我,我将非常感激!

If anyone could help me out here, I would be very grateful!

推荐答案

请参见gridExtra软件包中的?panel.xblocks:

See ?panel.xblocks in the latticeExtra package:

library(latticeExtra)
x <- 1:100
xyplot( rnorm(100) ~ x, type="l", col="black") + 
         layer_(panel.xblocks(x, x > 20, col = "lightgrey"))

这篇关于如何在R中的xyplot中获取带阴影的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆