ChiLiubio / microeco

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

assign named colors to 'plot_sum_links' #356

Closed chrissy005 closed 5 months ago

chrissy005 commented 5 months ago

Dear Chi Liu,

I am aware that color values can be passed as a list to the 'plot_sum_links' function. However, I am unable to assign specific colors to specific taxa. Will it be possible to do this. I managed to do this for the 'plot_taxa_roles' function.

I am hoping for something like the following:

color_values_1 = c("Cyanobacteriia"="#3B4D16","Bacteroidia"="#950D3D", "Chloroflexia"="#848F22", "Blastocatellia"="#A7A3FE","Alphaproteobacteria" ="#B2C9B2", "Unassigned Bacteria (Kingdom)" = "#8CCBD3", "Deinococci" = "#0DD7FD", "Planctomycetes"= "#99D584","Gammaproteobacteria" = "#0D8CA8","Anaerolineae" = "#DFC500")

microeco_transnetwork_spieceasi$plot_sum_links(plot_pos = TRUE ,plot_num = 10, color_values = color_values_1, groupnameFontsize = 5.5, showTicks = FALSE, groupnamePadding = 2)

ChiLiubio commented 5 months ago

Hi. It is a better way to provide an ordered colors vector instead of named vector like this.

library(magrittr)
library(microeco)
data(dataset)

test1 <- trans_network$new(dataset = dataset, cor_method = "bray", filter_thres = 0.003)
test1$cal_network(COR_p_thres = 0, COR_cut = 0.3)
test1
test1$cal_module()
test1$cal_sum_links()
test1$plot_sum_links()
# delete the prefix
rownames(test1$res_sum_links_pos) %<>% gsub("p__", "", .)
colnames(test1$res_sum_links_pos) %<>% gsub("p__", "", .)
# just for the example using three phylum
color_values_1 = c("Chloroflexi"="red", "Proteobacteria"="blue", "Bacteroidetes"="green")
plot_num = 3
color_values_1 = color_values_1[rownames(test1$res_sum_links_pos)[1:plot_num]] %>% unname
test1$plot_sum_links(plot_pos = TRUE , plot_num = plot_num, color_values = color_values_1)
chrissy005 commented 5 months ago

This works perfectly. Thank you.