Can't organise the sample order for Bar-plots

Hello everyone!
We got data from 16s sequencing.
I performed analysis with qiime2, then tried to use R for further analysis.

So, I have created phyloseq object with next code:
metadata<-read_q2metadata("/path/metadata.tsv", header = TRUE)
features<-read_qza("/path/table.qza")
taxonomy<-read_qza("/path/taxonomy.qza")

taxonomy<-parse_taxonomy(taxonomy$data)
head(taxonomy)
phyloseq2<-qza_to_phyloseq(features = "/path/table.qza",
tree = "/path/rooted-tree.qza",
metadata = " /path/metadata.tsv",
taxonomy = "/path/taxonomy.qza")
taxonomy = as.matrix(taxonomy)

I got:

phyloseq
phyloseq-class experiment-level object
otu_table() OTU Table: [ 3262 taxa and 72 samples ]
sample_data() Sample Data: [ 72 samples by 5 sample variables ]
tax_table() Taxonomy Table: [ 3262 taxa by 7 taxonomic ranks ]
phy_tree() Phylogenetic Tree: [ 3262 tips and 3261 internal nodes ]

Now I can do bar plots, but I need to assign order for our samples.
I tried next thing:
sample_data(phyloseq)$SampleID<-factor(sample_data(phyloseq)$SampleID, levels = c("order of our samples, each sample separately"))

And i got this error:
Error in $<-.data.frame(S3Part(x, TRUE), name, value) :
replacement has 0 rows, data has 72

How can I fix it?
I would appreciate any help.
I am a beginner in R.
Thanks

I tried to reproduce your error; I could only do so by mis-stating the name from which the samples are ; so review that ...

library("phyloseq"); packageVersion("phyloseq")

data(soilrep)
sample_data(soilrep)
mylevels <- levels(sample_data(soilrep)$Sample)

#works
sample_data(soilrep)$Sample <- factor(sample_data(soilrep)$Sample ,
                                      levels=rev(mylevels))

#gives the error
sample_data(soilrep)$SampleNOTRIGHTNAME <- factor(sample_data(soilrep)$SampleNOTRIGHTNAME ,
                                      levels=rev(mylevels))

Thank you.
It didn't worked unfortunately.

Error in rev(" some sample names ", :
unused arguments (" again all sample names"

rev is arbitrary; it reverses a list of values; in this case the old levels.
I dont know your levels or even what you wrote in there ...
The way you quoted your error implies that you literally tried to reverse the text
" some sample names " and "again all sample names"
I don't know to interpret this...

Oh, sorry for tat

Error in rev("1rA", "1rB", "1rC", "2rA", "2rB", "2rC", "3rA", "3rB", "3rC", :
unused arguments ("1rB", "1rC", "2rA", "2rB", "2rC", "3rA", "3rB", "3rC", "4rA", "4rB"

  • sample id from metadata file I have 72 samples

Maybe this would be helpful

if you want to use rev; to reverse the order of your levels; if you are writing them out as a list by hand; you would need to use the c() function to collate them

rev(c( "1rA", "1rB", "1rC", "2rA", "2rB", "2rC", "3rA "  # ... etc)

Thank you
Got the same error

Error in $<-.data.frame(S3Part(x, TRUE), name, value) :
replacement has 0 rows, data has 72

ok; I think you are confused about what my code was about;
you didnt provide data; and its hard to fix bugs in code without data.
I provided example data. it has levels which are relevent to that data; your data is different.
it serves no purpose to use part of my example in your true code; as if one directly relates to the other
my example code was didactic ; showing how factors can be correctly adjusted - for a given data,
and how a mistake such as not correctly knowing the name of your sample column, can cause the sort of error you faced.
I did not intend to teach you how to use rev.

your screenshot does not show me anything called SampleID that you may have been trying to use.
I didnt see any sign of labels such as "1rA" ; so I'm confused as to if you have such labels anywhere; even if they might not be in the right area....
I see BarcodeSequence things; Group, Treatment, Day ...
I don't know what these mean to you. but presumably you know ?
did you think that one of these was called SampleID?

Ok
Sample Id or names have meaning number is the number of the rat, r - rat, A, B, C - it's different time points, when the samples were taken.
I can save my data and share, if it will help.

Oh, what a pity.
I can't upload file here...
Only images

lets walk real through the issue slowly.
in my working example; I could investigate the names of the columns like so

names(sample_data(soilrep))

when you do the equivalent ; what are the names ?

names(sample_data(phyloseq))

Thank you)

names(sample_data(phyloseq))
[1] "BarcodeSequenceForward" "BarcodeSequenceReverse"
[3] "Group" "Treatment"
[5] "Day"

I used all those objects to create the phyloseq object

head(metadata)
X.SampleID BarcodeSequenceForward BarcodeSequenceReverse Group Treatment Day
1 #q2:types categorical categorical categorical categorical numeric
2 1rA CCTAAACTACGG GAGGAGTAAAGCCTA high threshold sensitivity stress 0
3 1rB GTGGTATGGGAGT GAGGAGTAAAGCCTA high threshold sensitivity stress 7
4 1rC TGTTGCGTTTCTGT GAGGAGTAAAGCCTA high threshold sensitivity stress 24
5 2rA TGCAGATCCAAC GAGGAGTAAAGCCTA high threshold sensitivity stress 0
6 2rB ACTTTAAGGGTGT GAGGAGTAAAGCCTA high threshold sensitivity stress 7

head(taxonomy)
Kingdom Phylum
a1263f992ddcdf8bcc4c6d1be739fc97 "Bacteria" "Firmicutes"
be99709f30998aa717415c21a440a05a "Bacteria" "Firmicutes"
92689a665d0e2db5f82b763338f764c0 "Bacteria" "Firmicutes"
46ba079e3dd450003995dbc763bf33bc "Bacteria" "Firmicutes"
2a62fb7ded30811592bde54de69614cc "Bacteria" "Firmicutes"
ddf4bfd7438a09020aa1e67342219067 "Bacteria" "Firmicutes"
Class Order
a1263f992ddcdf8bcc4c6d1be739fc97 "Bacilli" "Lactobacillales"
be99709f30998aa717415c21a440a05a "Bacilli" "Lactobacillales"
92689a665d0e2db5f82b763338f764c0 "Bacilli" "Lactobacillales"
46ba079e3dd450003995dbc763bf33bc "Bacilli" "Lactobacillales"
2a62fb7ded30811592bde54de69614cc "Bacilli" "Lactobacillales"
ddf4bfd7438a09020aa1e67342219067 "Bacilli" "Lactobacillales"
Family
a1263f992ddcdf8bcc4c6d1be739fc97 "Lactobacillaceae"
be99709f30998aa717415c21a440a05a "Lactobacillaceae"
92689a665d0e2db5f82b763338f764c0 "Lactobacillaceae"
46ba079e3dd450003995dbc763bf33bc "Lactobacillaceae"
2a62fb7ded30811592bde54de69614cc "Lactobacillaceae"
ddf4bfd7438a09020aa1e67342219067 "Lactobacillaceae"
Genus Species
a1263f992ddcdf8bcc4c6d1be739fc97 "Lactobacillus" NA
be99709f30998aa717415c21a440a05a "Lactobacillus" "salivarius"
92689a665d0e2db5f82b763338f764c0 "Lactobacillus" NA
46ba079e3dd450003995dbc763bf33bc "Lactobacillus" NA
2a62fb7ded30811592bde54de69614cc "Lactobacillus" NA
ddf4bfd7438a09020aa1e67342219067 "Lactobacillus" NA

head(features)
$uuid
[1] "2dc0f890-697c-418c-8df8-1284c02abd47"

$type
[1] "FeatureTable[Frequency]"

$format
[1] "BIOMV210DirFmt"

$contents
files.Name
1 2dc0f890-697c-418c-8df8-1284c02abd47/checksums.md5
2 2dc0f890-697c-418c-8df8-1284c02abd47/metadata.yaml
3 2dc0f890-697c-418c-8df8-1284c02abd47/VERSION
4 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/metadata.yaml
5 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/citations.bib
6 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/VERSION
7 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/artifacts/b7366270-65b9-4c14-8e67-e9baa5527118/metadata.yaml
8 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/artifacts/b7366270-65b9-4c14-8e67-e9baa5527118/citations.bib
9 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/artifacts/b7366270-65b9-4c14-8e67-e9baa5527118/VERSION
10 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/artifacts/b7366270-65b9-4c14-8e67-e9baa5527118/action/action.yaml
11 2dc0f890-697c-418c-8df8-1284c02abd47/provenance/action/action.yaml
12 2dc0f890-697c-418c-8df8-1284c02abd47/data/feature-table.biom
files.Length files.Date size
1 806 2022-01-03 21:05:00 4096
2 96 2022-01-03 21:05:00 4096
3 39 2022-01-03 21:05:00 4096
4 96 2022-01-03 21:05:00 4096
5 4305 2022-01-03 21:05:00 4096
6 39 2022-01-03 21:05:00 4096
7 139 2022-01-03 20:50:00 4096
8 2774 2022-01-03 20:50:00 4096
9 39 2022-01-03 20:50:00 4096
10 4952 2022-01-03 20:50:00 4096
11 5806 2022-01-03 21:05:00 4096
12 228152 2022-01-03 21:05:00 4096

$version
V1 V2
1 QIIME 2
2 archive: 5
3 framework: 2021.4.0

$data
10rA 10rB 10rC 11rA 11rB 11rC
a1263f992ddcdf8bcc4c6d1be739fc97 16 0 0 44 0 0
be99709f30998aa717415c21a440a05a 81 0 0 112 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 55 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 34 0 0
2a62fb7ded30811592bde54de69614cc 18 0 0 35 0 0
ddf4bfd7438a09020aa1e67342219067 0 0 0 0 0 0
2d533c98239b8e83872490dc5cbdf076 22 0 0 26 0 0
bcc1333773ed3cfe94f92d6925ccad3c 29 0 0 33 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 22 0 0
6c074d70b2061a09868443fba9a05433 0 0 0 0 0 0
31a5585f454fed22e2d03c4522eca1da 0 0 0 0 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 0 0 0 0 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 223 0 0 174
12rA 12rB 12rC 13rA 13rB 13rC
a1263f992ddcdf8bcc4c6d1be739fc97 89 0 0 0 0 0
be99709f30998aa717415c21a440a05a 237 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 86 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 55 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 71 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 0 0 0 118 0 0
2d533c98239b8e83872490dc5cbdf076 59 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 103 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 60 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 0 0 0 173 0 0
31a5585f454fed22e2d03c4522eca1da 0 0 0 229 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 0 0 0 158 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 406 0 0 0
14rA 14rB 14rC 15rA 15rB 15rC
a1263f992ddcdf8bcc4c6d1be739fc97 0 0 0 0 0 0
be99709f30998aa717415c21a440a05a 0 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 0 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 80 0 0 0 0 0
2d533c98239b8e83872490dc5cbdf076 0 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 0 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 140 0 0 0 0 0
31a5585f454fed22e2d03c4522eca1da 131 0 0 70 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 75 0 0 28 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 0 0 0 0
16rA 16rB 16rC 17rA 17rB 17rC
a1263f992ddcdf8bcc4c6d1be739fc97 0 0 0 0 0 0
be99709f30998aa717415c21a440a05a 0 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 0 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 453 0 0 319 0 0
2d533c98239b8e83872490dc5cbdf076 0 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 0 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 194 0 0 179 0 0
31a5585f454fed22e2d03c4522eca1da 219 0 0 138 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 300 0 0 178 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 0 0 0 0
18rA 18rB 18rC 19rA 19rB 19rC
a1263f992ddcdf8bcc4c6d1be739fc97 0 0 0 0 0 0
be99709f30998aa717415c21a440a05a 0 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 0 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 226 0 0 136 0 0
2d533c98239b8e83872490dc5cbdf076 0 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 0 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 110 0 0 193 0 0
31a5585f454fed22e2d03c4522eca1da 110 0 0 139 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 131 0 0 110 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 0 0 0 0
1rA 1rB 1rC 20rA 20rB 20rC 21rA
a1263f992ddcdf8bcc4c6d1be739fc97 0 0 0 0 0 0 0
be99709f30998aa717415c21a440a05a 0 0 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 0 0 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 0 0 0 102 0 0 65
2d533c98239b8e83872490dc5cbdf076 0 0 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 0 0 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 0 0 0 207 0 0 54
31a5585f454fed22e2d03c4522eca1da 0 0 0 127 0 0 40
b73ce1c91ef372e1d9c1d26c95b1ef25 0 0 0 143 0 0 39
8ed6d26f65a634635bb0f09223f55043 0 0 0 0 0 0 0
21rB 21rC 22rA 22rB 22rC 23rA
a1263f992ddcdf8bcc4c6d1be739fc97 0 0 0 0 0 0
be99709f30998aa717415c21a440a05a 0 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 0 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 0 0 0 0 0 0
2d533c98239b8e83872490dc5cbdf076 0 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 0 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 0 0 0 0 0 0
31a5585f454fed22e2d03c4522eca1da 0 0 0 0 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 0 0 0 0 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 0 0 0 0
23rB 23rC 24rA 24rB 24rC 2rA 2rB
a1263f992ddcdf8bcc4c6d1be739fc97 0 0 0 0 0 0 0
be99709f30998aa717415c21a440a05a 0 0 0 0 0 0 0
92689a665d0e2db5f82b763338f764c0 0 0 0 0 0 0 0
46ba079e3dd450003995dbc763bf33bc 0 0 0 0 0 0 0
2a62fb7ded30811592bde54de69614cc 0 0 0 0 0 0 0
ddf4bfd7438a09020aa1e67342219067 0 0 0 0 0 0 0
2d533c98239b8e83872490dc5cbdf076 0 0 0 0 0 0 0
bcc1333773ed3cfe94f92d6925ccad3c 0 0 0 0 0 0 0
3d0184e735fc8206c5ff73cd4211261b 0 0 0 0 0 0 0
6c074d70b2061a09868443fba9a05433 0 0 0 0 0 0 0
31a5585f454fed22e2d03c4522eca1da 0 0 0 0 0 0 0
b73ce1c91ef372e1d9c1d26c95b1ef25 0 0 0 0 0 0 0
8ed6d26f65a634635bb0f09223f55043 0 0 0 0 0 0 0

Also, maybe important

head(phyloseq)
Error in x[seq_len(n)] : object of type 'S4' is not subsettable

so there is no SampleID present here to adjust; we agree ?
That entirely explains the error your initially reported.
From what you shared; SampleID is something that appears in metadata ;or is X.SampleID ; hard to see

Sure)
Agree
Thank you very much
What can we do to fix it?
There is a part of metadata

Sorry I don't know; I'm not a phyloseq user and haven't studied their workflow.

A quick answer...

To do barplots 16S data, I do not recommend using phyloseq which is better for quick analysis. I usually export the data from phyloseq to data frames and plot the data frames using dplyr and ggplot2. It is much more flexible...

Also just having a quick look at your code, I will not use factors to recode the Samples names in PhyloSeq because in factors recode a set of values to 1, 2, 3 etc... and I am not sure Phyloseq will like that.

Thank you for your answer, but I don't use phyloseq to build barplots.

I used this tutorial

It worked, but the sample order is not what we need.
How can I turn my phyloseq S4 object into data.frame with the specific order of samples, then?
Thanks

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.