Question: How to sort the Q matrix from structure analyses based on countires first and then within the countires sort by q value
1
haneenih720 wrote:

Hi all,

I am using tess3r package to plot bar plots of the Q matrix of ancestry coefficients that I got from sNMF analysis.

But I am interested to order the matrix based on counties first and then based on Q, in another word, I want to have the matrix ordered by countires and within the conuty ordered by Q.

For example this is a q matrix, I've added the county for each individuals

``````1.00E-04    0.75453 1.00E-04    0.24507 1.00E-04    1.00E-04    CM04487 Benin
1.00E-04    0.880116    0.0482435   0.0713402   1.00E-04    1.00E-04    CM04489 Mali
0.0822559   0.805366    0.00713594  1.00E-04    0.0198168   0.0853258   CM04493 Benin
0.967625    1.00E-04    1.00E-04    1.00E-04    1.00E-04    0.0319755   CM06496 Burkina Faso
0.83507 0.0272506   1.00E-04    0.00335877  1.00E-04    0.134121    CM06501 Burkina Faso
0.435478    0.235613    1.00E-04    0.131601    0.0401917   0.157016    CM06505 Guinea
0.302861    0.313093    0.0068676   1.00E-04    0.00161187  0.375466    CM06510 Mali
0.335162    0.365021    0.0119398   0.0315991   0.0107296   0.245548    CM06511 Ghana
0.502391    0.0369072   1.00E-04    0.0648057   0.0577006   0.338095    CM06513 Burkina Faso
1.00E-04    0.734269    0.000945988 0.259324    0.00526157  1.00E-04    CM07885 Guinea
``````

in tess3r package running this part won't give what I want, because it sorts based on Q only.

``````barplot(Q.matrix, sort.by.Q = FALSE,
border = NA, space = 0,
col.palette = my.palette,
xlab = "Individuals", ylab = "Ancestry coefficients") -> bp
``````

Guid and help is much appriciated.

Thanks!

population genetics R • 118 views
modified 7 weeks ago • written 7 weeks ago by haneenih720
3
benformatics1.1k wrote:

Order by column 8 first and then "sub"-order by column 7

``````Q.matrix <- Q.matrix[order(Q.matrix[,8],Q.matrix[,7]),]
``````

EDIT: Actually this might be wrong... what column is Q? If it's not a variable in your data.frame it will be hard to use it for ordering.

Otherwise you can split your data.frame based on country with `split(Q.matrix,Q.matrix[,8])` and then plot each country separately (using `lapply` or a `for` loop).

If you have names for your columns you can do it with those too.

e.g. Q.matrix <- Q.matrix[order(Q.matrix\$country,Q.matrix\$Q,]

I don't have the q value in my data frame. However, I can calculate the q value and use the code line to sort.

Thank you very much!