When I try to use the function below, I notice that my top row is missing the horizontal line in the plot. I’ve tracked this down to the line that specifies my rect dimensions, specifically how high and low it should be. Is there a parameter I can set so that the top line always appears? The code below does not have the top line..
Plot.Lines <- function(colCount, rowCount, cex){
colCount <- colCount # number per row
rowCount <- rowCount
plot( c(1,colCount), c(0,rowCount), type="n", ylab="", xlab="",
axes=FALSE, ylim=c(rowCount,0))
title("My Lines")
for (j in 0:(rowCount-1))
{
base <- j*colCount
remaining <- length(colors()) - base
RowSize <- ifelse(remaining < colCount, remaining, colCount)
for(i in 1:RowSize){
rect(i-0.5,j-0.5,i+0.5, j+0.5, border = "black", col = colors()[base + (1:RowSize)])
}
for(i in 1:RowSize){
text(x = i, y =j, labels = paste(base + i), cex =cex, col = "black")
}
}
}
Plot.Lines(25, 6, cex = 0.5)
However, if I change the line to:
rect(i-0.5,j-0.2,i+0.5, j+0.2, border = "black", col = colors()[base + (1:RowSize)])
then it works, although there is a space between the rows. Is there a way I an set it automatically given the colCount and the rowCount?
The top edge is cut off because it’s beyond the plotting region. You can use
par(xpd=TRUE)to allow plotting outside the inner region. Put this before your call toplot.