ChiLiubio / microeco

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

same color for different levels of a factor in different plots #221

Closed apoosakkannu closed 1 year ago

apoosakkannu commented 1 year ago

Hi, To assign the same colors to each country in different plots, even when some countries are not present in all plots,

could you please give me idea of how to assign same colors for different levels of a factor? example figure below,

image

ChiLiubio commented 1 year ago

Please use color_values parameter to adjust the color palette in each plot. You should select corresponding colors to assign color_values since the legend are different.

apoosakkannu commented 1 year ago

Thanks. As I am using the loop it was a bit difficult for me. the code is following,

library(ggplot2) library(patchwork) library(magrittr) library(gridExtra)

Convert Country column to a factor with desired order of levels

dataset2$sample_table$Country <- factor(dataset2$sample_table$Country, levels = c("Finland", "France", "Latvia", "Denmark", "Poland"))

Set base font size for all plots

theme_set(theme_gray(base_size = 15))

Create an empty list to store the plots

alphaplot_list <- list()

Calculate and plot the alpha diversity

t_country_alpha <- trans_alpha$new(dataset = dataset2, group = "Country", by_group = "Host_taxa") t_country_alpha$cal_diff(method = "wilcox") country_chao <- t_country_alpha$plot_alpha(measure = "Chao1", add_sig = FALSE) + lims(y = c(0, 3500)) country_shannon <- t_country_alpha$plot_alpha(measure = "Shannon", add_sig = FALSE) + lims(y = c(0, 8))

Add the plots to the list

alphaplot_list[[1]] <- country_chao alphaplot_list[[2]] <- country_shannon

Display the plots in a grid

alpha_plot<-gridExtra::grid.arrange(grobs = alphaplot_list, ncol = 2)

extract unique taxa names

allgroups <- unique(dataset2$sample_table[, "Host_taxa"])

Create an empty list to store the plots

betaplot_list <- list()

Create an empty list to store the PERMANOVA results for each group

permanova_list <- list()

Create an empty list to store the beta dispersion results for each group

dispersion_list <- list()

Iterate over all groups

for (i in allgroups) {

Subset the dataset based on the current group

tmp <- clone(dataset2) tmp$sample_table <- subset(tmp$sample_table, Host_taxa == i) tmp$tidy_dataset()

Add the group name to the plot title

plot_title <- paste(i)

Compute beta diversity and perform PCoA

tmp_beta <- trans_beta$new(dataset = tmp, group = "Country", measure = "bray") tmp_beta$cal_ordination(ordination = "PCoA")

Plot the ordination and store the result in the list

betaplot_list[[i]] <- tmp_beta$plot_ordination(plot_color = "Country", plot_shape = "Country", plot_type = c("point", "ellipse")) + ggtitle(plot_title)

Perform PERMANOVA and store the results in the list

tmp_beta$cal_manova(manova_all = TRUE)

Add the results data frame to the list

permanova_list[[i]] <- tmp_beta$res_manova

Perform beta dispersion and store the results in the list

tmp_beta$cal_betadisper()

Add the results data frame to the list

dispersion_list[[i]] <- tmp_beta$res_betadisper$tab

Remove intermediate objects

rm(tmp, tmp_beta, tmp_df) }

Combine the PERMANOVA results for all groups into a single data frame

permanova_df <- do.call(rbind, permanova_list)

Write the PERMANOVA results to a CSV file

write.csv(permanova_df, "permanova_results.csv", row.names = TRUE)

Combine the beta dispersion results data frames into a single data frame

dispersion_df <- do.call(rbind, dispersion_list)

Write the beta dispersion results to a CSV file

write.csv(dispersion_df, file = "beta_dispersion_results.csv", row.names = TRUE)

Create a single figure with all plots arranged

beta_plot<-wrap_plots(betaplot_list, ncol = 2)

Tidy up

rm(allgroups, plot_list, permanova_list, dispersion_list)

Create an empty list to store the plots

countryplot_list <- list()

Add the plots to the list

countryplot_list[[1]] <- alpha_plot countryplot_list[[2]] <- beta_plot

Create a single figure with all plots arranged

country_plot <- wrap_plots(countryplot_list, ncol = 1)

On Fri, Apr 14, 2023 at 4:47 AM Chi Liu @.***> wrote:

Please use color_values parameter to adjust the color palette in each plot. You should select corresponding colors to assign color_values since the legend are different.

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

ChiLiubio commented 1 year ago

You can allocate colors in the color_values parameter according to your groups selected to show in the figure no matter what style you are using.