填充绘图中水平线上方和下方的区域 [英] Fill area above and below horizontal lines in plot

查看:51
本文介绍了填充绘图中水平线上方和下方的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充两条水平线上方和下方的区域.

I would like to fill the area above and below the two horizontal lines.

这是我想出来的:

 par(mfrow=c(1,2))
 x<-seq(1,24,1)
 y<-rnorm(24, 10, 2)

 for(i in 1:2) {
 plot(x,y,ylim=c(4,16))
 lines(x,y)
 abline(h=11)
 abline(h=9)}

 hyper<-y
 hyper[hyper<11]<-11
 polygon(x,hyper,col="gray")

我的主要问题是与水平线的交点不对.

My main problem is that the intersect with the horizontal line is not right.

推荐答案

如果您仍然希望使用老式 graphics 工具(plot, ablinelines 等 - 其他建议涉及 grid 系统衍生物,例如 ggplot2),您可以尝试使用自定义剪辑区域进行播放,请参阅 ?clip:

If you still wish to make the figure with the old-school graphics facilities (plot, abline, lines, and so on - the other suggestions concern the grid system derivatives, like ggplot2), you may try to play with a custom clipping region, see ?clip:

par(mfrow=c(1, 2))
x <- seq(1, 24, 1)
y <- rnorm(24, 10, 2)

# 1st plot
plot(x, y, ylim=c(4,16), type='o')

# 2nd plot
plot(x, y, type='n', ylim=c(4,16))

clip(x1=min(x),x2=max(x), y1=11, y2=max(y))
polygon(c(min(x), x, max(x)), c(min(y), y, min(y)), col="gray")

clip(x1=min(x),x2=max(x), y1=9, y2=min(y))
polygon(c(min(x), x, max(x)), c(max(y), y, max(y)), col="gray")

clip(par("usr")[1], par("usr")[2], par("usr")[3], par("usr")[4]) # reset clipping region
lines(x,y, type='o')
abline(h=c(9, 11))

首先我们设置没有绘图的绘图区域,然后我们设置两个不同的剪辑区域(我们用灰色填充在其中绘图),然后我们移除剪辑区域并重新用线和点绘图.

First we set up the plot region with no plotting, then we set up two different clipping regions (into which we plot with grey fills), then we remove the clipping region and re-did plotting with lines and points.

这篇关于填充绘图中水平线上方和下方的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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