{"id":589,"date":"2024-03-23T20:38:19","date_gmt":"2024-03-23T12:38:19","guid":{"rendered":"https:\/\/linguopeng.top\/?p=589"},"modified":"2024-03-23T20:44:01","modified_gmt":"2024-03-23T12:44:01","slug":"pcoaboxplot","status":"publish","type":"post","link":"https:\/\/linguopeng.top\/?p=589","title":{"rendered":"PCoA+boxplot"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># rm(list=ls())\n# # If devtools package is not installed, first install it\n# #install.packages(\"devtools\")\n# #devtools::install_github(\"ChiLiubio\/microeco\")\n# pacman::p_load(microeco,tidytree,treeio,ggplot2,ggtree,microeco)\n# # Let's create a microtable object with more information\n# dataset &lt;- microtable$new(sample_table = sample_info_16S,\n#                           otu_table = otu_table_16S,\n#                           tax_table = taxonomy_table_16S,\n#                           phylo_tree = phylo_tree_16S)\n# sample_table&lt;-dataset$sample_table\n# otu_table&lt;-dataset$otu_table\n# tax_table&lt;-dataset$tax_table\n# phylo_tree&lt;-dataset$phylo_tree\n# write.csv(otu_table,\"feature_table.csv\")\n# write.csv(sample_table,\"sample_table.csv\")\n# write.csv(tax_table,\"tax_table.csv\")\n# write.tree(as.phylo(phylo_tree),\"phylo_tree_16S.nwk\")\n#\u53c2\u8003\u94fe\u63a5\uff1ahttps:\/\/mp.weixin.qq.com\/s\/xbuycJN52fZhP8zNG1eJ9A\n##Step1\uff1a\u521b\u5efamicrotable\u5bf9\u8c61\nrm(list=ls())\npacman::p_load(microeco,tidytree,treeio,ggplot2,ggtree,microeco,aplot)\nfeature_table &lt;- read.csv('feature_table.csv', row.names = 1)\nsample_table &lt;- read.csv('sample_table.csv', row.names = 1)\ntax_table &lt;- read.csv('tax_table.csv', row.names = 1)\n# \u521b\u5efamicrotable\u5bf9\u8c61\ndataset &lt;- microtable$new(sample_table = sample_table,\n                          otu_table = feature_table, \n                          tax_table = tax_table)\ndataset\nhead(sample_table)\nhead(feature_table)\nhead(tax_table)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/1-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1255\" height=\"911\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/1-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-595\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>##Step2\uff1aPCoA\u5206\u6790\n# PCoA\ndataset$cal_betadiv()\nt1 &lt;- trans_beta$new(dataset = dataset, group = \"Group\", measure = \"bray\")\nt1$cal_ordination(ordination = \"PCoA\")\ntmp &lt;- t1$res_ordination$scores\n# differential test with trans_env class\nt2 &lt;- trans_env$new(dataset = dataset, add_data = tmp&#91;, 1:2])\n# 'KW_dunn' for non-parametric test\nt2$cal_diff(group = \"Group\", method = \"anova\")\nt2\n#\u4e3b\u56fe\np1 &lt;- t1$plot_ordination(plot_color = \"Group\",\n                         plot_shape = \"Group\",\n                         plot_type = c(\"point\", \"ellipse\"))\n###PCo1 \u7bb1\u578b\u56fe1  \np2 &lt;- \n  t2$plot_diff(measure = \"PCo1\", add_sig = T) + \n  theme_bw() + \n  coord_flip() + \n  theme(legend.position = \"none\", \n        axis.title.x = element_blank(), \n        axis.text.y = element_blank(), \n        axis.ticks.y = element_blank())\n###PCo2 \u7bb1\u578b\u56fe2\np3 &lt;- t2$plot_diff(measure = \"PCo2\", add_sig = T) + \n  theme_bw() + \n  theme(legend.position = \"none\", \n        axis.title.y = element_blank(), \n        axis.text.x = element_blank(), \n        axis.ticks.x = element_blank())\n### \u7ec4\u5408\u56fe\ng &lt;- p1 %&gt;% \n  insert_top(p2, height = 0.2) %&gt;% \n  insert_right(p3, width = 0.2)\ng<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/Rplot.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"495\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/Rplot.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-591\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u4e0a\u56fe\u7684\u6c34\u5e73\u8f74\u548c\u53f3\u4fa7\u56fe\u7684\u5782\u76f4\u8f74\u4e0e\u4e3b\u56fe\u7684\u8f74\u7ebf\u5e76\u4e0d\u5b8c\u5168\u5bf9\u5e94\n####\u624b\u52a8\u63a7\u5236\u5750\u6807\u8303\u56f4\n#\u5bf9PCOA\u7684\u4e3b\u6210\u5206\u5750\u6807\u7f29\u653e\uff0c\u7edf\u4e00\u5750\u6807\nx_lim &lt;- range(tmp&#91;, 1]) * 1.4\ny_lim &lt;- range(tmp&#91;, 2]) * 1.4\np1 &lt;- p1 + scale_y_continuous(limits = y_lim, expand = c(0, 0)) + \n  scale_x_continuous(limits = x_lim, expand = c(0, 0))\np2 &lt;- p2 + scale_y_continuous(limits = x_lim, expand = c(0, 0))\np3 &lt;- p3 + scale_y_continuous(limits = y_lim, expand = c(0, 0))\n\ng1 &lt;- p1 %&gt;% \n  insert_top(p2, height = 0.2) %&gt;% \n  insert_right(p3, width = 0.2)\ng1<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/Rplot01.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"495\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/Rplot01.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-592\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5207\u6362\u4e3b\u9898\np2 &lt;- p2 + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())\np3 &lt;- p3 + theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())\ng2 &lt;- (p1 + theme_classic()) %&gt;% \n  insert_top(p2, height = 0.15) %&gt;% \n  insert_right(p3, width = 0.15)\ng2<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/Rplot02.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"495\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/03\/Rplot02.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-593\"\/><\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-589","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=589"}],"version-history":[{"count":3,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/589\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}