logicstar
logicstar

Reputation: 197

White turns black during polygon drawing

I am plotting polygon using following code. I need to plot polygon with no color so I am keeping #FFFFFF as color code in last column for it. But when i pot the polygon using following R code i get black color instead.

data<-read.table("data.txt",sep="\t");
x1<-data$V1
x2<-data$V3
x3<-data$V5
x4<-data$V7
y1<-data$V2
y2<-data$V4
y3<-data$V6
y4<-data$V8
color<-data$V9
xx<-c(x1,x2,x3,x4)
yy<-c(y1,y2,y3,y4)

xmin<-min(x1,x2,x3,x4)
xmax<-max(x1,x2,x3,x4)
ymin<-min(y1,y2,y3,y4)
ymax<-max(y1,y2,y3,y4)


jpeg("myplot.jpg")
plot(xx,yy, type='n',xlim=c(xmin, xmax), ylim=c(ymin, ymax), xaxt='n', ann=FALSE, yaxt='n')
for (i in 1:nrow(data))
{  
   xx=c(x1[i],x2[i],x3[i],x4[i])
   yy=c(y1[i],y2[i],y3[i],y4[i])
   polygon(xx, yy, col=color[i], border="white", xaxt='n', ann=FALSE)
}
dev.off();

data.txt

61573287.5  15104   61573101.5  14732   61572879    15177   61573065    15549   "#FFFFFF" 
61573402.5  15334   61573287.5  15104   61573065    15549   61573180    15779   "#FFFFFF" 
61573690.5  15910   61573402.5  15334   61573180    15779   61573468    16355   "#FFA500" 
61573972.75 16474.5 61573690.5  15910   61573468    16355   61573750.25 16919.5 "#FFA500" 
61574133    16795   61573972.75 16474.5 61573750.25 16919.5 61573910.5  17240   "#FFA500" 
61574293.25 17115.5 61574133    16795   61573910.5  17240   61574070.75 17560.5 "#FFFFFF"

enter image description here

How can i plot empty polygon?

Upvotes: 2

Views: 65

Answers (1)

while
while

Reputation: 3772

This is because the color column is interpreted as a factor and not strings. This happens when you read data frames in general.

sapply(data, class)
#        V1        V2        V3        V4        V5        V6        V7        V8        V9 
# "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"  "factor" 

If you supply a factor as a col attribute to a plot function (here polygon) it will use the standard palette to color the data.

palette()
# [1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow"  "gray

Try setting your color column to character as so:

color <- as.character(data$V9)

and then plot the data.

Edit:

If it didn't work for you you must have some problem reading the data or you changed something.

Here is my example running the same code you supplied w my fix:

data<-read.table("data.txt",sep="\t")
data
#         V1      V2       V3      V4       V5      V6       V7      V8      V9
# 1 61573288 15104.0 61573102 14732.0 61572879 15177.0 61573065 15549.0 #FFFFFF
# 2 61573402 15334.0 61573288 15104.0 61573065 15549.0 61573180 15779.0 #FFFFFF
# 3 61573690 15910.0 61573402 15334.0 61573180 15779.0 61573468 16355.0 #FFA500
# 4 61573973 16474.5 61573690 15910.0 61573468 16355.0 61573750 16919.5 #FFA500
# 5 61574133 16795.0 61573973 16474.5 61573750 16919.5 61573910 17240.0 #FFA500
# 6 61574293 17115.5 61574133 16795.0 61573910 17240.0 61574071 17560.5 #FFFFFF 

and then running

x1<-data$V1
x2<-data$V3
x3<-data$V5
x4<-data$V7
y1<-data$V2
y2<-data$V4
y3<-data$V6
y4<-data$V8
color<-as.character(data$V9)
xx<-c(x1,x2,x3,x4)
yy<-c(y1,y2,y3,y4)

xmin<-min(x1,x2,x3,x4)
xmax<-max(x1,x2,x3,x4)
ymin<-min(y1,y2,y3,y4)
ymax<-max(y1,y2,y3,y4)

jpeg("test.jpg")
plot(xx,yy, type='n',xlim=c(xmin, xmax), ylim=c(ymin, ymax), xaxt='n', ann=FALSE, yaxt='n')
for (i in 1:nrow(data))
{  
   xx=c(x1[i],x2[i],x3[i],x4[i])
   yy=c(y1[i],y2[i],y3[i],y4[i])
   polygon(xx, yy, col=color[i], border="white", xaxt='n', ann=FALSE)
}
dev.off()

This gives me the following picture: enter image description here

Upvotes: 1

Related Questions