Entering edit mode
                    7.1 years ago
        leeandroid
        
    
        ▴
    
    150
    Hi everyone,
I'm using DAPC to analyze the population structure of a GBS dataset. I was able to generate a table with the following:
                           Population   Accession Assigned   Membership
    1                     C. a      jal         1   9.051345e-63
    2                     C. a      ancho       2   1.396897e-10
    3                     C. b      toro        3   0.000000e+00
    4                     C. b      b37         1   0.000000e+00
    5                     C. c      mis         1   0.000000e+00
    6                     C. a      jal         2   9.051345e-63
    7                     C. a      ancho       1   1.396897e-10
    8                     C. b      toro        4   0.000000e+00
    9                     C. b      b37         2   0.000000e+00
    10                    C. c      mis         3   0.000000e+00
...
Then I used ggplot to plot the compoplot (Structure-like):
cap.color <- brewer.pal(n = 7, name = "Set3")
ggplot(cap.dapc, aes(x=Accession, y=Membership, fill=Assigned)) + 
  geom_bar(stat='identity') +
  scale_fill_manual(values = cap.color) + 
  facet_grid(~Population, scales = "free", space = "free_x") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 0, vjust = 0.5)) +
  xlab("Accession")
That results in a plot ordered alphabetically by sample name and by population. My goal is to change the code in order to samples appear order by the Assigned category and Membership. How can I do that?
I've tried using
cap.dapc$Accession <- factor(cap.dapc$Assigned, levels = rev(unique(cap.dapc$Accession)), ordered=TRUE)
If it is a generic order thing in ggplot2 checkout https://kohske.wordpress.com/2010/12/29/faq-how-to-order-the-factor-variables-in-ggplot2/
you are missing
cap.colorin test data and sample number. Barplot is faceted by population. Hence all your plots will be faceted by population categories (factors), in this case a, b and c. Within each population, x axis has accession and y -axis has membership. Hence you can manipulate the order of accession (x-axis) only. Is that what is required? leeandroidI've just added the color palette to my original post. You're right, I can only change the accession order. Another option would be changing the layout of the plot entirely, but I can't think of a clearer way to put it.
well, whatever you have mind, you can put it here. some one will understand it and provide a solution. That is how we learn here. leeandroid
factor every column which you want to order, then feed combination in facet_grid(), you can take help from this post
I've tried factor both Assigned and Membership but it didn't work. I'm not sure I put it correctly:
I'm not sure if my example data is clear enough for you guys to get the picture. Each accession appears Assigned 7 times and the Membership column has the probability of that assignation (which could be from 0 to 1).
Thanks for your help!