> Following the given codes below, I generated a plot that has 6 regions around a
> center point (IL), with 5 regions containing
> a point (L1, L2 to L5) and sixth vacant region. I want background of all the filled
> regions turned "green", while "red" for the
> vacant region. Can it be done through a quicker way?
You can certainly reduce the number of lines of code.
And you can shade a region using polygon()
Code to replace yours and add polygon below, as an example, commented.
plot(1:10,type="n",xlab=expression("D"[a]),ylab=expression("D"[b]),cex.lab=1.3, mgp = c(2, 1, 0))
#type="n" supporesses points - no need to draw them at all
pu <- par("usr") #saves typing later
#shading regions is easiest using polygon()
#You also need the shading first, to draw on top.
#since you're shading everything except one region, start with a single background colour.
rect(pu,pu,pu,pu,col = "lightgreen")
#construct and add the shaded region
xrange <- pu-pu
yrange <- pu-pu
polygon(x.poly, y.poly, col="pink") #shaded region
#Then there are simplifications using vectorisation:
# points(), text() and segments() are vectorised so you only need one call to each.
px <- c(5,5,5,2,9,8)
py <- c(5,9,1,7,4,8)
p.cols <- c("green", rep("yellow", 5))
points(px, py, pch=19, col=p.cols)
# You can put text at the point locations with a simple offset
# You can also construct text expressions by substitution
for(i in 1:6)
text(px[i]+0.5, py[i]+0.3, bquote(L[.(i)]), cex=1.2)
# segments() takes vectors too.
# The following is just an example, assuming you want equally spaced lines at
# 60 degree intervals, all originating from a centre (x0=5, y0=5)
seglen <- 10 #arbitrary length, relying on clipping to the plot region