Reputation: 1432
i have the following code to demonstrate that sometimes lines in r on plots don't draw. I can't find the pattern, or issue preventing this, I have even reversed coordinates as can be seen in my 5th and 6th line attempts, tried within plot bounds etc, for the life of me can't understand this
Any advice would be appreciated.
myColors=c("red","green","blue","orange","pink","purple")
df<-setNames(data.frame(matrix(ncol = 3, nrow = 0)), c("Person","yearsExp", "yearsStudy"))
df[nrow(df) + 1,] = c(1,0,0)
df[nrow(df) + 1,] = c(2,0,3)
df[nrow(df) + 1,] = c(3,3,0)
df[nrow(df) + 1,] = c(4,3,3)
plot(df$yearsExp,df$yearsStudy,xlab=xlab,ylab=ylab,main=title,pch=pchvector,col=colvector,type="n",xlim=c(0,3),ylim=c(0,3))
text(df$yearsExp,df$yearsStudy,labels=df$Person)
# will draw
lines(c(1.9,1.9), c(2.0,2.4), pch=16, col=myColors[3],type="l")
lines(c(1.3,1.2), c(1.8,1.1), pch=16, col=myColors[2],type="l")
lines(c(1.7,1.1), c(1.3,1.9), pch=16, col=myColors[1],type="l")
# wont draw
lines(c(0.0,0.0), c(3.0,3.0), pch=16, col=myColors[6],type="l")
lines(c(0.1,0.1), c(1.3,1.3), pch=16, col=myColors[5],type="l")
lines(c(1.3,1.3),c(0.1,0.1), pch=16, col=myColors[4],type="l")
Upvotes: 0
Views: 224
Reputation: 50728
I think you misunderstood the x
and y
input of lines
.
lines(c(0.0, 0.0), c(3.0, 3.0))
draws a line from (0, 3)
to (0, 3)
which is single point.
I guess you meant
lines(c(0, 3), c(0, 3))
which draws a straight line between points (0, 0)
and (3, 3)
.
For example:
plot(df$yearsExp,df$yearsStudy,type="n",xlim=c(-1,4),ylim=c(-1,4))
text(df$yearsExp,df$yearsStudy,labels=df$Person)
lines(c(0, 3), c(0, 3))
Upvotes: 2