0

I generate an isolation-by-distance among populations graph with R. On this kind of graph, you have on the x-axis a geographical distance and on the y-axis a genetic distance separating a pair of populations (ie each point corresponds to a pair of populations separated by a certain geographic distance and a certain genetic distance). To produce the graph, I use two matrices, one giving geographical distances for each pair of populations, the other genetic distances for each pair as well. My matrices therefore have both in column and row the name of my populations.

I generate a simple plot : plot(Dgeo[lower.tri(Dgeo)], Dgen[lower.tri(Dgen)], xlab = "d_geo", ylab = "d_gen", pch=20, cex=.5, col="cadetblue4", ylim =c(0, 1))

Each of my populations is part of a certain geographical distribution area among three. My aim would be to colour the points according to a condition that refers to this: if the two populations being compared are from the same geographical distribution area, the point corresponding to this pair is of a certain colour; if one of the populations is from geographical area A and the other from B, then the point is of another colour; and so on. Do you have an idea how to code this in R? I am grateful for the time you will spend thinking about this together.

Bye

I have tried to create a matrice like the two others containing a different letter for each condition and I used the argument col = my_colour_matrix, but without success

  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 02 '23 at 15:12

1 Answers1

0

Generate a color matrix cols containing your distribution area interactions with the same dimensions as your data matrices, then use these for plotting.

Toy data

set.seed(42)
cols <- matrix(colors()[sample(600, 20)], nrow=5)
dd <- matrix(sample(20), nrow=5)
de <- matrix(sample(20), nrow=5)
plot(dd[lower.tri(dd)], de[lower.tri(de)], 
  xlab = "d_geo", ylab = "d_gen",  pch=19, cex=2, col=cols[lower.tri(cols)])

R scatter plot

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29