Knight tour using geom_path, unexpected plot for matrix with a 5 digit number in it

Hi, I use the following code to create kinght tour visualizations:

df <- read.csv("D:\\knightTours\\RothN.csv")
Mat = as.matrix(df)
var.size = n
coords <- tibble(col = rep(1:var.size, var.size),
                 row = rep(1:var.size, each = var.size))

coords %>% 
  mutate(order = Mat[var.size * col + row]) %>%  
  arrange(order) %>% 
  ggplot(aes(x = col, y = row )) + 
  geom_path() + 

Each csv file contains n * n amount of numbers: 0, 1, 2, 3, ... n * n

However when n > 99, the plot is not looking as expected, more precisly, I found out that the chaos occurs when there is any 5 digit number in the .csv file.
here is the plot for n = 99:

and here is the plot for n = 100:

in the same n = 100 plot, replacing 10000 digit with 0, while connecting 0 with 1, produces a plot correct with the numbering within the csv file, which cemented me in the belief of 5 digit number being a problem:

the issue grows only larger with bigger n, here is n = 125:

The plot looks as expected for all n <= 99, why the appearance of 5 digits number in the matrix destroys the whole plot ?

Are you sure about that line? col and row start counting at 1, so the smallest coordinate you look up is n*1+1 = n+1 and you never read the Mat cell at [1,1]. I think you should use:

mutate(order = Mat[var.size * (col-1) + row]) %>%  

or equivalently:

mutate(order = map2_int(row, col, ~ Mat[.x, .y])) %>%

Not totally sure without data to test on. If that's not the solution, you should also double-check the type of the order column, make sure it doesn't get arranged by alphabetical order or something.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.