
请参考上图.我在excel中绘制了方程式,然后手工着色.你可以看到它不是很整洁.您可以看到有六个区域,每个区域由两个或更多个方程组成.使用阴影图案绘制不等式和阴影区域的最简单方法是什么?
pla*_*pus 16
为了建立@ agstudy的答案,这里有一种快速而肮脏的方式来表示R中的不等式:
plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
with(as.list(c(b,a)),{
id <- yB<=yA
# b<a area
polygon(x = c(xB[id], rev(xA[id])),
y = c(yB[id], rev(yA[id])),
density=10, angle=0, border=NULL)
# a>b area
polygon(x = c(xB[!id], rev(xA[!id])),
y = c(yB[!id], rev(yA[!id])),
density=10, angle=90, border=NULL)
})
Run Code Online (Sandbox Code Playgroud)

如果有问题的区域被2个以上的方程所包围,只需添加更多条件:
plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
d <- curve(0.5*x^2+0.2, add = TRUE) # Third curve
names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
names(d) <- c('xD','yD')
with(as.list(c(a,b,d)),{
# Basically you have three conditions:
# curve a is below curve b, curve b is below curve d and curve d is above curve a
# assign to each curve coordinates the two conditions that concerns it.
idA <- yA<=yD & yA<=yB
idB <- yB>=yA & yB<=yD
idD <- yD<=yB & yD>=yA
polygon(x = c(xB[idB], xD[idD], rev(xA[idA])),
y = c(yB[idB], yD[idD], rev(yA[idA])),
density=10, angle=0, border=NULL)
})
Run Code Online (Sandbox Code Playgroud)

ags*_*udy 10
在R中,填充图案只有有限的支持,它们只能应用于矩形和多边形.这只是在传统图形中,没有ggplot2或lattice.
可以使用以特定角度绘制的一组线填充矩形或多边形,线之间具有特定的间隔.甲密度 参数控制所述线之间的间隔(以每英寸的线而言)和角度参数控制所述线的角度.
这里有一个帮助的例子:
plot(c(1, 9), 1:2, type = "n")
polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
density = c(10, 20), angle = c(-45, 45))
Run Code Online (Sandbox Code Playgroud)

编辑
另一种选择是使用alpha混合来区分区域.这里使用@plannapus示例和gridBase包来叠加多边形,你可以这样做:
library(gridBase)
vps <- baseViewports()
pushViewport(vps$figure,vps$plot)
with(as.list(c(a,b,d)),{
grid.polygon(x = xA, y = yA,gp =gpar(fill='red',lty=1,alpha=0.2))
grid.polygon(x = xB, y = yB,gp =gpar(fill='green',lty=2,alpha=0.2))
grid.polygon(x = xD, y = yD,gp =gpar(fill='blue',lty=3,alpha=0.2))
}
)
upViewport(2)
Run Code Online (Sandbox Code Playgroud)
