ChiLiubio / microeco

An R package for data analysis in microbial community ecology
GNU General Public License v3.0
181 stars 55 forks source link

problem in getting desired levels of a factor in facet_wrap of rarefaction curve #277

Open apoosakkannu opened 9 months ago

apoosakkannu commented 9 months ago

Hi, I have problem in getting desired levels of a factor in facet_wrap of rarefaction curve even after i changed the levels of factor in dataset itself, the code is following,

upload the required data for pacbio 16S rRNA gene amplicon analysis

creation and cleanup of dataset object for downstream analyses

set the seed for reproducibility of some of the random analysis below

set.seed(123)

upload all the data files

upload the OTU abundance file

abund_table<-read.csv("/scratch/project_2003832/pacbio_rawdata/bird_bat/asv_stat/all_decontam_asv/samples_asv_abundtable_decontam_all.csv",row.names=1,check.names=FALSE)

upload the meta data file

meta_table<-read.csv("/scratch/project_2003832/pacbio_rawdata/bird_bat/asv_stat/all_decontam_asv/samples_asv_metatable_20221213.csv",row.names=1,check.names=FALSE)

load the taxonomy

OTU_taxonomy<-read.csv("/scratch/project_2003832/pacbio_rawdata/bird_bat/asv_stat/all_decontam_asv/samples_asv_taxtable_decontam_all.csv",row.names=1,check.names=FALSE)

make the taxonomic information unified, very important #require "tidytree" and "microeco" packages

library(tidytree) #version 0.3.5 library(microeco) #version 0.11.0

OTU_taxonomy %<>% tidy_taxonomy

load the representative sequences ##require biostrings package

library(Biostrings) rep_seq<-readDNAStringSet(filepath="/scratch/project_2003832/pacbio_rawdata/bird_bat/asv_stat/all_nondecontam_asv/samples_asv_repseq.fasta", format="fasta", nrec=-1L, skip=0L, seek.first.rec=FALSE, use.names=TRUE)

create and cleanup data object #The packages "microeco" and "tidytree" uploaded already

create a dataset of all the data uploaded

dataset <- microtable$new(sample_table = meta_table, otu_table = abund_table, tax_table = OTU_taxonomy, rep_fasta = rep_seq, auto_tidy = TRUE)

make the OTU and sample information consistent across all files in the dataset object

dataset$tidy_dataset()

dataset

remove control

remember first clone the full dataset

dataset1 <- clone(dataset) dataset1$sample_table <- subset(dataset1$sample_table, Host_taxa != "Control")

make the object tidy

dataset1$tidy_dataset() dataset1$tidy_dataset()

dataset1

str(dataset1$sample_table$Host_taxa)

Convert a character variable to a factor

Change the levels of the Host_taxa factor variable

dataset1$sample_table$Host_taxa <- factor( dataset1$sample_table$Host_taxa, levels = c("Black bird", "Mallard", "Wood pigeon", "Nathusius bat") )

rarefaction analysis

library(microeco) library(mecodev) library(ggplot2)

set.seed is used to fix the random number generation to make the results repeatable

set.seed(123)

trans_rarefy class

t1 <- trans_rarefy$new(dataset1, alphadiv = "Observed", depth = c(0, 10, 50, 500, 1000))

Change the levels of the Host_taxa factor variable

t1$dataset$sample_table$Host_taxa <- factor( t1$dataset$sample_table$Host_taxa, levels = c("Black bird", "Mallard", "Wood pigeon", "Nathusius bat") )

library(ggplot2)

rarefaction <- t1$plot_rarefy(color = "Host_taxa", show_point = FALSE, add_fitting = FALSE) + facet_wrap(~Host_taxa, nrow = 1) + theme( panel.background = element_rect(fill = "transparent", color = NA), plot.background = element_rect(fill = "transparent", color = NA), text = element_text(size = 20) # Set the desired font size for all text elements )

Display the plot

print(rarefaction)

ChiLiubio commented 9 months ago

Hi. I used the example data to test the steps and found it is fine. So if an issue is still there, please make the plot using ggplot2 by extracting the rarefying data results, i.e. t1$res_rarefy. The following is my test steps.

library(microeco)
library(mecodev)
data(sample_info_16S)
data(otu_table_16S)
set.seed(123)
dataset <- microtable$new(sample_table = sample_info_16S, otu_table = otu_table_16S)
dataset$tidy_dataset()
# trans_rarefy class
t1 <- trans_rarefy$new(dataset, alphadiv = "Observed", depth = c(0, 10, 50, 500, 2000, 4000, 6000, 8000))
g1 <- t1$plot_rarefy(color = "Group", show_point = FALSE, add_fitting = FALSE)
g1 + facet_wrap(~ Group, nrow = 1) + theme(
    panel.background = element_rect(fill = "transparent", color = NA),
    plot.background = element_rect(fill = "transparent", color = NA),
    text = element_text(size = 20) # Set the desired font size for all text elements
)
apoosakkannu commented 9 months ago

thanks. please find the attached my dataset for your reference. Currently I am getting facet wrapped figures in the order of black bird, mallard, nathusius bat and wood pigeon. But I need them in the order of black bird, mallard, wood pigeon and nathusius bat. dataset1.rdata.zip https://drive.google.com/file/d/10C3H_Nx5dKpngafwabK4evZ3XbQHqVRg/view?usp=drive_web

On Fri, Sep 29, 2023 at 3:55 AM Chi Liu @.***> wrote:

Hi. I used the example data to test the steps and found it is fine. So if an issue is still there, please make the plot using ggplot2 by extracting the rarefying data results, i.e. t1$res_rarefy. The following is my test steps.

library(microeco) library(mecodev) data(sample_info_16S) data(otu_table_16S) set.seed(123) dataset <- microtable$new(sample_table = sample_info_16S, otu_table = otu_table_16S) dataset$tidy_dataset()

trans_rarefy class

t1 <- trans_rarefy$new(dataset, alphadiv = "Observed", depth = c(0, 10, 50, 500, 2000, 4000, 6000, 8000)) g1 <- t1$plot_rarefy(color = "Group", show_point = FALSE, add_fitting = FALSE) g1 + facet_wrap(~ Group, nrow = 1) + theme( panel.background = element_rect(fill = "transparent", color = NA), plot.background = element_rect(fill = "transparent", color = NA), text = element_text(size = 20) # Set the desired font size for all text elements )

— Reply to this email directly, view it on GitHub https://github.com/ChiLiubio/microeco/issues/277#issuecomment-1740171430, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMC3WZUG6CTF6PGFDUDEE3LX4YMBDANCNFSM6AAAAAA5LIBUBE . You are receiving this because you authored the thread.Message ID: @.***>