Question: Plot factors by specific column in ggplot
0
gravatar for leeandroid
2.4 years ago by
leeandroid110
leeandroid110 wrote:

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)
dapc order R • 693 views
ADD COMMENTlink modified 2.4 years ago • written 2.4 years ago by leeandroid110

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/

ADD REPLYlink written 2.4 years ago by Sej Modha4.8k

you are missing cap.color in 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? leeandroid

ADD REPLYlink written 2.4 years ago by cpad011215k

I'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.

ADD REPLYlink written 2.4 years ago by leeandroid110

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

ADD REPLYlink written 2.4 years ago by cpad011215k

factor every column which you want to order, then feed combination in facet_grid(), you can take help from this post

ADD REPLYlink written 2.4 years ago by Prakash2.1k

I've tried factor both Assigned and Membership but it didn't work. I'm not sure I put it correctly:

cap.dapc$Membership <- factor(cap.dapc$Membership, levels(factor(cap.dapc$Membership)))
cap.dapc$Assigned <- factor(cap.dapc$Assigned, levels(factor(cap.dapc$Assigned)))

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!

ADD REPLYlink modified 2.4 years ago • written 2.4 years ago by leeandroid110
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1121 users visited in the last hour
_