Shading specific region in R

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Shading specific region in R

Eliza Botto-2
Dear useRs,

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?

Thanks in advance

###Codes start from here################################

plot(1:10,col="white",xlab=expression("D"[a]),ylab=expression("D"[b]),cex.lab=1.3, mgp = c(2, 1, 0))

rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "white")

points(5,5,pch=19,col="green",lwd=2)

points(5,9,pch=19,col="yellow",lwd=2)

points(5,1,pch=19,col="yellow",lwd=2)

points(2,7,pch=19,col="yellow",lwd=2)

points(9,4,pch=19,col="yellow",lwd=2)

points(8,8,pch=19,col="yellow",lwd=2)

text(5.7,9.5,expression("L"[1]),cex=1.2)

text(8.7,8.5,expression("L"[2]),cex=1.2)

text(9.6,4.5,expression("L"[3]),cex=1.2)

text(5.6,1.5,expression("L"[4]),cex=1.2)

text(2.4,7.7,expression("L"[5]),cex=1.2)

text(5.5,4.4,expression("I"[L]),cex=1.2)

X1<-5

Y1<-5

X2<-5*X1

Y2<-Y1

base<-sqrt((X1-X2)^2+(Y1-Y2)^2)

Hyp =base/cos(60*pi/180)

Pre= Hyp*sin(60*pi/180)

Y3<-Pre+Y2

X3<-X2

segments((X3), (Y3), (X1-base), (Y1-Pre),type="l",col="red")

segments((X1-base), (Y3), (X3), (Y1-Pre),type="l",col="red")

segments((X2), (Y2), (X1-base), (Y1),type="l",col="red")



regards,

Eliza



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Shading specific region in R

S Ellison-2
> 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[1],pu[3],pu[2],pu[4],col = "lightgreen")

#construct and add the shaded region
xrange <- pu[3]-pu[1]
yrange <- pu[4]-pu[2]

x0 <- 5
y0 <- 5

x.poly <- c(x0, pu[1], pu[1], x0 - (y0 - pu[3]) / tan(pi/3), x0)
y.poly <- c(y0, y0, pu[3], pu[3], y0)

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

segments(x0, y0, x0+seglen * cos( (0:5) * pi/3), y0 +seglen * sin( (0:5) * pi/3)  )



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.