BaseballR
BaseballR

Reputation: 147

Draw R shape from 4 x,y points on a plot

I am trying to fill weird shapes with conditional colors and having trouble finding a way to do this. Ideally there would be a shape builder that takes in 4 points each x,y that fills in the area between these points.

Like shape(x1,y1,x2,y2,x3,y3,x4,y4,col='black')

Here is the code I am using to build the plot:

 plot(1,1,type='n',xlim=c(-250,250),ylim=c(0,420),cex.main=2.25,xaxt='n',yaxt='n',xlab="",ylab="") 
segments(0,0,-283,283,lwd=3.8)
segments(0,0,283,283,lwd=3.8)
library('plotrix')
segments(sqrt(16200)/2,sqrt(16200)/2,-(300-(-sqrt(16200)/2))/tan(45),350,lty=2)
segments(-sqrt(16200)/2,sqrt(16200)/2,(300-(-sqrt(16200)/2))/tan(45),350,lty=2)
segments(0,sqrt(16200),-112.25,400,lty=2)
segments(0,sqrt(16200),112.25,400,lty=2)
segments(0,sqrt(16200),0,420,lty=2)
draw.arc(0, 0, 250, deg1=47,deg2 =132, col = "black",lwd=1.8,lty=2)
draw.arc(0, 0, 300, deg1=47,deg2 =132, col = "black",lwd=1.8,lty=2)
draw.arc(0, 0, 350, deg1=47,deg2 =132, col = "black",lwd=1.8,lty=2)
draw.arc(0, 0, 400, deg1=47,deg2 =132, col = "black",lwd=1.8,lty=2)

So inside each of these shapes I would like a conditional color based on how many things I have from another dataset. It is easy to build the color formatting but wondering best way to fill these weird shapes!

Here is what that code produces: field

Thank you!

Upvotes: 0

Views: 439

Answers (1)

gtwebb
gtwebb

Reputation: 3011

The function you are looking for is polygon

x<-c(0,0,2,1)
y<-c(0,1,2,0)
plot(1, type="n", axes=T, xlab="", ylab="",xlim=c(0,2),ylim=c(0,2))
polygon(x,y,col="red")

enter image description here

Upvotes: 2

Related Questions