在情节()

时间:2016-08-24 13:27:28

标签: r plot ggplot2 vector-graphics

我试图绘制一张CDF来显示与水质标准相关的水质数据。

为了使水质标准明显,我使用rect()来指示图的哪些部分不在设定的标准范围内。

我在rect()内使用密度来创建一个散列填充,以便在以黑白打印时效果更好(我觉得它看起来更好)。

当我只使用我想要使用的情节的一个区域rect()时,这很好用。

one_rect() in plot

当我在绘图中有两个单独的rect()调用时,第一个rect()似乎忽略了密度参数并使用了实心填充:

two_rect in plot

以下是我用来执行此操作的代码示例:

pH数据
data<-rnorm(30, mean = 7, sd = 1) 
pH值的图表
data.ecdf<-ecdf(data)
plot(data.ecdf, verticals = TRUE, do.points = FALSE, xaxs = "i", yaxs = "i", xlim=c(5,10), panel.first = rect(5, -0.02, 6, 1.04, density = 20, border = "258", col = "258"))

plot(data.ecdf, verticals = TRUE, do.points = FALSE, xaxs = "i", yaxs = "i", xlim=c(5,10), panel.first = rect(5, -0.02, 6, 1.04, density = 20, border = "258", col = "258", rect(9,-0.02,10,1.04, density = 20, border = "258", col = "258")))

我也尝试在ggplot2中这样做,但似乎得到一个散列填充比在图形中困难得多。

关于我如何在背景中同时显示rect()以显示散列填充(density = 20)的任何想法或想法将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

不确定为什么会发生这种情况,但你可以在panel.first arg中同时调用两个矩形:

plot(data.ecdf, verticals = TRUE, do.points = FALSE, xaxs = "i", yaxs = "i", xlim=c(5,10), panel.first = c(
  rect(9,-0.02,10,1.04, density = 20, border = "258", col = "258"),
  rect(5, -0.02, 6, 1.04, density = 20, border = "258", col = "258")
))

或者,尝试将矩形作为图层独立调用:

plot(data.ecdf, verticals = TRUE, do.points = FALSE, xaxs = "i", yaxs = "i", xlim=c(5,10), lty='n')
rect(9,-0.02,10,1.04, density = 20, border = "258", col = "258")
rect(5, -0.02, 6, 1.04, density = 20, border = "258", col = "258")
lines(data.ecdf, verticals = TRUE, do.points = FALSE, xaxs = "i", yaxs = "i", xlim=c(5,10))

enter image description here