سؤال

I would like to exploit something like the "polygon" function to fill the area under a curve but I don't want to use colors but narrow parallel lines (better if oblique).

Is that possible?

هل كانت مفيدة؟

المحلول

Using the density and angle arguments this can be achieved in polygon() (i.e. read the manual)

 x=seq(-7,10,length=200)
 y1=dnorm(x,mean=0,sd=1)
 plot(x,y1,type="l",lwd=2,col="red")
 y2=dnorm(x,mean=3,sd=2)
 lines(x,y2,type="l",lwd=2,col="blue")


 polygon(x,pmin(y1,y2), density = 10, angle = -45)

with MINOR tweaking from (Shaded area under two curves using R)

enter image description here

نصائح أخرى

It is not clear what do you mean by narrow parallel lines. But It is possible to fill a polygon with a set of lines drawn at a certain angle, with a specific separation between the lines. A density argument controls the separation between the lines (in terms of lines per inch) and an angle argument controls the angle of the line. Her an example where I hash the region defined by the intersection of 2 polygons:

plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2-0.25, add = TRUE) # First curve
b <- curve(0.5-x^4, 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=60, border=NULL)
    # a>b area
    polygon(x = c(xB[!id], rev(xA[!id])),
                    y = c(yB[!id], rev(yA[!id])), 
                    density=10, angle=30, border=NULL)
})

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top