Plot factors by specific column in ggplot
0
0
Entering edit mode
5.6 years ago
leeandroid ▴ 130

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 R order • 1.4k views
ADD COMMENT
0
Entering edit mode
ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 2226 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6