{"id":731,"date":"2024-06-26T11:49:51","date_gmt":"2024-06-26T03:49:51","guid":{"rendered":"https:\/\/linguopeng.top\/?p=731"},"modified":"2024-06-26T11:49:51","modified_gmt":"2024-06-26T03:49:51","slug":"%e8%b7%9f%e7%9d%80imeta%e5%ad%a6%e5%81%9a%e5%9b%be%ef%bd%9cggplot2%e7%bb%98%e5%88%b6%e7%9b%b8%e5%85%b3%e6%80%a7%e5%88%86%e6%9e%90%e7%ba%bf%e9%9d%a2%e7%bb%84%e5%90%88%e7%83%ad%e5%9b%be","status":"publish","type":"post","link":"https:\/\/linguopeng.top\/?p=731","title":{"rendered":"\u8ddf\u7740iMeta\u5b66\u505a\u56fe\uff5cggplot2\u7ed8\u5236\u76f8\u5173\u6027\u5206\u6790\u7ebf\u9762\u7ec4\u5408\u70ed\u56fe"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># \u519c\u5fc3\u751f\u4fe1\u5de5\u4f5c\u5ba4\n# 2023-01-18\n# \u672c\u6587\u4ee3\u7801\u5df2\u7ecf\u4e0a\u4f20\u81f3https:\/\/github.com\/iMetaScience\/iMetaPlot230118corr \u5982\u679c\u4f60\u4f7f\u7528\u672c\u4ee3\u7801\uff0c\n# \u8bf7\u5f15\u7528\uff1aChangchao Li. 2023. Destabilized microbial networks with distinct performances of abundant and rare biospheres \n# in maintaining networks under increasing salinity stress. iMeta 1: e79. https:\/\/onlinelibrary.wiley.com\/doi\/10.1002\/imt2.79\n# \u4ee3\u7801\u7f16\u5199\u53ca\u6ce8\u91ca\uff1a\u519c\u5fc3\u751f\u4fe1\u5de5\u4f5c\u5ba4\n# \u76f8\u5173\u6027\u70ed\u56fe (Correlation Heatmap) \u7684\u4f7f\u7528\u5728\u5fae\u751f\u7269\u7ec4\u7814\u7a76\u4e2d\u975e\u5e38\u666e\u904d\uff0c\u5c24\u5176\u662f\u7ebf\u9762\u7ec4\u5408\u7684\u76f8\u5173\u6027\u70ed\u56fe\uff0c\n# \u5176\u4e2d\u7684\u76f8\u5173\u6027\u70ed\u56fe\u901a\u5e38\u8868\u793a\u73af\u5883\u56e0\u5b50\u95f4\u7684Pearson\u76f8\u5173\u7cfb\u6570\uff0c\u8fde\u7ebf\u5219\u8868\u793a\u7269\u79cd\u7ec4\u6210\u4e0e\u5404\u73af\u5883\u56e0\u5b50\u7684Mantel\u76f8\u5173\u6027\u3002\n# \u672c\u671f\u6211\u4eec\u6311\u90092023\u5e741\u67089\u65e5\u520a\u767b\u5728iMeta\u4e0a\u7684Destabilized microbial networks with distinct performances of \n# abundant and rare biospheres in maintaining networks under increasing salinity stress\uff0c\n# \u9009\u62e9\u6587\u7ae0\u7684Figure 1C\u8fdb\u884c\u590d\u73b0\uff0c\u8bb2\u89e3\u548c\u63a2\u8ba8\u5982\u4f55\u57fa\u4e8eggplot2\u7ed8\u5236\u7ebf\u9762\u7ec4\u5408\u7684\u76f8\u5173\u6027\u70ed\u56fe\uff0c\u5148\u4e0a\u539f\u56fe\uff1a\n<\/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\/06\/fig1.webp'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1400\" height=\"1160\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/06\/fig1.webp\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-732\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u8be6\u5c3d\u7684\u4ee3\u7801\u9010\u6b65\u62c6\u89e3\u539f\u56fe\uff0c\u6700\u7ec8\u5b9e\u73b0\u5bf9\u539f\u56fe\u7684\u590d\u73b0\u3002\n# R\u5305\u68c0\u6d4b\u548c\u5b89\u88c5\n# 1.\u5b89\u88c5\u6838\u5fc3R\u5305ggplot2\u4ee5\u53ca\u4e00\u4e9b\u529f\u80fd\u8f85\u52a9\u6027R\u5305\uff0c\u5e76\u8f7d\u5165\u6240\u6709R\u5305\noptions(repos = list(CRAN=\"https:\/\/mirrors.tuna.tsinghua.edu.cn\/CRAN\/\"))\nif (!require(\"ggplot2\"))\n  install.packages('ggplot2') \nif (!require(\"vegan\"))\n  install.packages('vegan')\nif (!require(\"tidyverse\"))\n  install.packages('tidyverse') \nif (!require(\"BiocManager\"))\n  install.packages('BiocManager') \nif (!require(\"ComplexHeatmap\"))\n  BiocManager::install('ComplexHeatmap')\n# \u68c0\u67e5\u5f00\u53d1\u8005\u5de5\u5177devtools\uff0c\u5982\u6ca1\u6709\u5219\u5b89\u88c5\nif (!require(\"devtools\"))\n  install.packages(\"devtools\")\n# \u52a0\u8f7d\u5f00\u53d1\u8005\u5de5\u5177devtools\nlibrary(devtools)\n# \u68c0\u67e5linkET\u5305\uff0c\u6ca1\u6709\u5219\u901a\u8fc7github\u5b89\u88c5\u6700\u65b0\u7248\nif (!require(\"linkET\"))\n  install_github(\"Hy4m\/linkET\", force = TRUE)\n# \u52a0\u8f7d\u5305\nlibrary(ggplot2)\nlibrary(vegan)\nlibrary(linkET)\nlibrary(tidyverse)\nlibrary(ComplexHeatmap)\n# \u8bfb\u53d6\u6570\u636e\u53ca\u6570\u636e\u5904\u7406\n# 2.\u7ed8\u5236\u7ebf\u9762\u7ec4\u5408\u76f8\u5173\u6027\u70ed\u56fe\uff0c\u9700\u8981\u73af\u5883\u56e0\u5b50\u6570\u636e\u548c\u7269\u79cd\u7ec4\u6210\u6570\u636e\u3002\u793a\u4f8b\u6570\u636e\u53ef\u5728GitHub\u4e0a\u83b7\u53d6\u3002\n#\u8bfb\u53d6\u6570\u636e\n#\u6839\u636e\u539f\u6587\u9644\u8868S1\uff0c\u83b7\u5f97\u6837\u54c1\u7684\u7269\u7406\u5316\u5b66\u7279\u6027\u548c\u5730\u7406\u5206\u5e03\u60c5\u51b5\uff0c\u4f5c\u4e3a\u73af\u5883\u56e0\u5b50\u8868\nenv&lt;-read.csv(\"env.CSV\",row.names = 1,header = T)\n#\u751f\u6210\u4e00\u4e2a\u7269\u79cd\u7ec4\u6210\u7684\u4e30\u5ea6\u8868\uff0c\u884c\u4e3a\u6837\u672c\uff0c\u5217\u4e3a\u7269\u79cd\uff0c\u884c\u6570\u5fc5\u987b\u4e0e\u73af\u5883\u56e0\u5b50\u8868\u7684\u884c\u6570\u76f8\u540c\nspec&lt;-read.csv(\"spec.CSV\",row.names = 1,header = T)\n# 3.\u8ba1\u7b97\u73af\u5883\u56e0\u5b50\u7684pearson\u76f8\u5173\u7cfb\u6570\uff0c\u5e76\u6839\u636e\u7ed8\u56fe\u9700\u6c42\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\ncorM &lt;- cor(env,method = \"pearson\")#\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u77e9\u9635\n#\u56e0\u4e3a\u9700\u8981\u7ed8\u5236\u7684\u662f\u4e0a\u4e09\u89d2\u70ed\u56fe\uff0c\u9700\u5bf9\u77e9\u9635\u8fdb\u884c\u5904\u7406\uff0c\u4fdd\u7559\u5bf9\u89d2\u7ebf\u53ca\u4e00\u534a\u7684\u6570\u636e\u5373\u53ef\nncr&lt;-nrow(corM)\nfor (i in 1:ncr){\n  for (j in 1:ncr){\n    if (j>i){\n      corM&#91;i,j]&lt;-NA\n    }\n  }\n}\ncorM&lt;-as.data.frame(corM)%>%mutate(colID=rownames(corM))\n#\u5bbd\u8868\u8f6c\u957f\u8868\ncorM_long&lt;-pivot_longer(corM,cols=-\"colID\",names_to = \"env\",values_to=\"cor\")%>%na.omit()\n#\u6839\u636e\u539f\u56fe\uff0c\u56fa\u5b9ax\u8f74\u548cy\u8f74\u7684\u987a\u5e8f\ncorM_long$colID&lt;-factor(corM_long$colID,levels = rownames(corM))\ncorM_long$env&lt;-factor(corM_long$env,levels = rev(rownames(corM)))\n# \u7ed8\u56fe\u9884\u89c8\n# 4.\u4f7f\u7528ggplot2\u5305\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u4e0a\u4e09\u89d2\u76f8\u5173\u6027\u70ed\u56fe\uff1a\np&lt;-ggplot(corM_long)+\n  geom_tile(aes(colID,env),fill=\"white\",color=\"grey\")+ #\u6dfb\u52a0\u65b9\u6846\n  geom_point(aes(colID,env,size=abs(cor),color=cor)) #\u6dfb\u52a0\u6563\u70b9<\/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\/06\/fig2.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"426\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/06\/fig2.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-733\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># 5.\u5bf9\u76f8\u5173\u6027\u70ed\u56fe\u8fdb\u884c\u7f8e\u5316\uff1a\ncol_fun&lt;-colorRampPalette(c(\"#FFC107\",\"white\",\"#3F51B5\"))(50) #\u8bbe\u7f6e\u6e10\u53d8\u8272\np&lt;-ggplot(corM_long)+\n  geom_tile(aes(colID,env),fill=\"white\",color=\"grey\")+ #\u6dfb\u52a0\u65b9\u6846\n  geom_point(aes(colID,env,size=abs(cor),fill=cor),color=\"black\",shape=21)+ #\u6dfb\u52a0\u6563\u70b9\n  scale_x_discrete(position = \"top\")+ #x\u8f74\u79fb\u52a8\u5230\u9876\u90e8\n  scale_y_discrete(position = \"right\")+ #y\u8f74\u79fb\u52a8\u5230\u53f3\u4fa7\n  scale_size_continuous(range = c(1,10))+\n  scale_fill_gradientn(colours=col_fun)+ #\u6e10\u53d8\u8272\u8bbe\u7f6e\n  theme(axis.ticks = element_blank(),\n        axis.title = element_blank(),\n        panel.background = element_blank())+ #\u53bb\u9664\u80cc\u666f\n  guides(size=\"none\",  #\u9690\u85cfsize\u56fe\u4f8b\n         fill=guide_colorbar(title=\"Pearson's r\"))  #\u4fee\u6539fill\u56fe\u4f8b\u6807\u9898\n\n<\/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\/06\/fig3.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"426\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/06\/fig3.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-734\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># 6.\u7528Mantel test\u68c0\u9a8c\u7269\u79cd\u7ec4\u6210\u548c\u4e0d\u540c\u73af\u5883\u56e0\u5b50\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528linkET\u5305\u4e2d\u7684mantel_test()\u51fd\u6570\u8f7b\u677e\u5b8c\u6210\u68c0\u9a8c\uff1a\nmantel&lt;-mantel_test(spec=spec,env = env)\n#spec\u53c2\u6570\u540e\u662f\u7269\u79cd\u7ec4\u6210\u6570\u636e\uff0cenv\u53c2\u6570\u540e\u662f\u73af\u5883\u56e0\u5b50\u6570\u636e\n# 7.\u751f\u6210\u7ed8\u5236\u8fde\u63a5\u5f27\u7ebf\u6240\u9700\u7684\u6570\u636e\u6846\u3002\u5305\u62ec\u8fde\u7ebf\u7684\u8d77\u70b9\u548c\u7ec8\u70b9\u5750\u6807\uff0c\u6839\u636emantel r\u7684\u503c\u786e\u5b9a\u7ebf\u6761\u7c97\u7ec6\uff0cmantel p\u7684\u503c\u786e\u5b9a\u7ebf\u6761\u989c\u8272\uff0c\uff1a\nn=nrow(corM)\ncurve_df&lt;-data.frame(\n  x0=rep(-1,n),\n  y0=rep(6,n),\n  x1=c(0:(n-1)-0.1),\n  y1=c(n:1)\n)%>%cbind(mantel)%>%\n  mutate(\n    line_col = ifelse(p &lt;= 0.001, '#D1C4E9', NA),  #\u6839\u636ep\u503c\u4e0d\u540c\u8bbe\u7f6e\u8fde\u63a5\u66f2\u7ebf\u7684\u989c\u8272\u4e0d\u540c\n    line_col = ifelse(p > 0.001 &amp; p &lt;= 0.01, '#607D8B', line_col),\n    line_col = ifelse(p > 0.01 &amp; p &lt;= 0.05, '#FF5722', line_col),\n    line_col = ifelse(p > 0.05, '#4CAF50', line_col),\n    linewidth = ifelse(r >= 0.4, 4, NA),\n    # \u6839\u636er\u503c\u4e0d\u540c\u8bbe\u7f6e\u8fde\u63a5\u66f2\u7ebf\u7684\u5bbd\u5ea6\u4e0d\u540c\n    linewidth = ifelse(r >= 0.2 &amp; r &lt; 0.4, 2, linewidth),\n    linewidth = ifelse(r &lt; 0.2, 0.8, linewidth)\n  )\n# 8.\u7ed8\u5236\u5e26\u6709\u8fde\u7ebf\u7684\u76f8\u5173\u6027\u70ed\u56fe\uff1a\np&lt;-ggplot(corM_long)+\n  geom_tile(aes(colID,env),fill=\"white\",color=\"grey\")+ #\u6dfb\u52a0\u65b9\u6846\n  geom_point(aes(colID,env,size=abs(cor),fill=cor),color=\"black\",shape=21)+ #\u6dfb\u52a0\u6563\u70b9\n  scale_x_discrete(position = \"top\")+ #x\u8f74\u79fb\u52a8\u5230\u9876\u90e8\n  scale_y_discrete(position = \"right\")+ #y\u8f74\u79fb\u52a8\u5230\u53f3\u4fa7\n  scale_size_continuous(range = c(1,10))+\n  scale_fill_gradientn(colours=col_fun)+ #\u6e10\u53d8\u8272\u8bbe\u7f6e\n  theme(axis.ticks = element_blank(),\n        axis.title = element_blank(),\n        panel.background = element_blank())+ #\u53bb\u9664\u80cc\u666f\n  guides(size=\"none\",  #\u9690\u85cfsize\u56fe\u4f8b\n         fill=guide_colorbar(title=\"Pearson's r\"))+  #\u4fee\u6539fill\u56fe\u4f8b\u6807\u9898\n  geom_curve(data=curve_df,aes(x=x0,y=y0,xend=x1,yend=y1), #\u6839\u636e\u8d77\u70b9\u7ec8\u70b9\u7ed8\u5236\u5f27\u7ebf\n             size=curve_df$linewidth, #\u6839\u636er\u503c\u8bbe\u7f6e\u7ebf\u6761\u5bbd\u5ea6\n             color=curve_df$line_col,  #\u6839\u636ep\u503c\u8bbe\u7f6e\u7ebf\u6761\u989c\u8272\n             curvature = 0.2)+ #\u8bbe\u7f6e\u5f27\u5ea6\n  geom_point(data = curve_df,aes(x=x1,y=y1),size=3,color=\"red\") #\u6dfb\u52a0\u8fde\u7ebf\u7ec8\u70b9\u7684\u70b9<\/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\/06\/fig4.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"426\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/06\/fig4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-735\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># 9.\u6211\u4eec\u53d1\u73b0\uff0c\u76ee\u524d\u7684\u56fe\u5f62\u7f3a\u5c11\u8fde\u7ebf\u989c\u8272\u548c\u7c97\u7ec6\u7684\u56fe\u4f8b\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u987e\u7956\u5149\u535a\u58eb\u5f00\u53d1\u7684ComplexHeatmap\u5305\uff08\u5173\u4e8eComplexHeatmap\u5305\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u53c2\u8003\u5f80\u671f\u63a8\u6587\u8ddf\u7740iMeta\u5b66\u505a\u56fe\uff5cComplexHeatmap\u7ed8\u5236\u591a\u6837\u7684\u70ed\u56fe\uff09\uff0c\u7ed8\u5236\u4e00\u4e2a\u5355\u72ec\u7684\u56fe\u4f8b\uff0c\u5e76\u7f6e\u4e8e\u753b\u5e03\u7684\u5de6\u4e0b\u65b9\uff1a\npdf(\"fig5.pdf\",width = 10.04, height = 6.26)\ngrid.newpage()\n#\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a1\u884c1\u5217\u7684\u5e03\u5c40\npushViewport(viewport(layout = grid.layout(nrow = 1, ncol = 1)))\nvp_value &lt;- function(row, col){\n  viewport(layout.pos.row = row, layout.pos.col = col)\n} \np1&lt;-ggplot(corM_long)+\n  geom_tile(aes(colID,env),fill=\"white\",color=\"grey\")+ #\u6dfb\u52a0\u65b9\u6846\n  geom_point(aes(colID,env,size=abs(cor),fill=cor),color=\"black\",shape=21)+ #\u6dfb\u52a0\u6563\u70b9\n  scale_x_discrete(position = \"top\")+ #x\u8f74\u79fb\u52a8\u5230\u9876\u90e8\n  scale_y_discrete(position = \"right\")+ #y\u8f74\u79fb\u52a8\u5230\u53f3\u4fa7\n  scale_size_continuous(range = c(1,10))+\n  scale_fill_gradientn(colours=col_fun)+ #\u6e10\u53d8\u8272\u8bbe\u7f6e\n  theme(axis.ticks = element_blank(),\n        axis.title = element_blank(),\n        panel.background = element_blank())+ #\u53bb\u9664\u80cc\u666f\n  guides(size=\"none\",  #\u9690\u85cfsize\u56fe\u4f8b\n         fill=guide_colorbar(title=\"Pearson's r\"))+  #\u4fee\u6539fill\u56fe\u4f8b\u6807\u9898\n  geom_curve(data=curve_df,aes(x=x0,y=y0,xend=x1,yend=y1), #\u6839\u636e\u8d77\u70b9\u7ec8\u70b9\u7ed8\u5236\u5f27\u7ebf\n             size=curve_df$linewidth, #\u6839\u636er\u503c\u8bbe\u7f6e\u7ebf\u6761\u5bbd\u5ea6\n             color=curve_df$line_col,  #\u6839\u636ep\u503c\u8bbe\u7f6e\u7ebf\u6761\u989c\u8272\n             curvature = 0.2)+ #\u8bbe\u7f6e\u5f27\u5ea6\n  geom_point(data = curve_df,aes(x=x1,y=y1),size=3,color=\"red\") #\u6dfb\u52a0\u8fde\u7ebf\u7ec8\u70b9\u7684\u70b9\n#\u5c06\u56fep1\u6dfb\u52a0\u8fdb\u753b\u5e03\nprint(p1,vp = vp_value(row = 1, col = 1))\n\n#\u5229\u7528ComplexHeatmap\u7684Legend()\u51fd\u6570\u7ed8\u5236\u5355\u72ec\u7684\u56fe\u4f8b\n#\u7ed8\u5236Mantel's p\u56fe\u4f8b\nlgd1 = Legend(at = 1:4, legend_gp = gpar(fill = c('#D1C4E9','#607D8B','#FF5722','#4CAF50')), \n              title = \"Mantel's p\",\n              labels =c(\"\u2264 0.001\",\"0.001 - 0.01\",\"0.01 - 0.05\",\"> 0.05\"),\n              nr = 1)\n#\u7ed8\u5236Mantel's r\u56fe\u4f8b\nlgd2 = Legend(labels =c(\"&lt; 0.02\",\"0.02 - 0.04\",\"\u2265 0.04\"),\n              title = \"Mantel's r\",\n              #graphics\u53c2\u6570\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u9ad8\u5ea6\n              graphics = list(\n                function(x, y, w, h) grid.rect(x, y, w, h*0.1*0.8, gp = gpar(fill = \"black\")),\n                function(x, y, w, h) grid.rect(x, y, w, h*0.1*2, gp = gpar(fill = \"black\")),\n                function(x, y, w, h) grid.rect(x, y, w, h*0.1*4, gp = gpar(fill = \"black\"))\n              ),nr = 1)\nlgd = packLegend(lgd1, lgd2)\n\n#\u5c06\u56fe\u4f8b\u6dfb\u52a0\u8fdb\u76f8\u5173\u6027\u70ed\u56fe\u4e2d\ndraw(lgd, x = unit(45, \"mm\"), y = unit(5, \"mm\"), just = c( \"bottom\"))\ndev.off()\n#> quartz_off_screen \n#>                 2\n<\/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\/06\/fig5.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"889\" height=\"550\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2024\/06\/fig5.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-736\"\/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u9644.\u5b8c\u6574\u4ee3\u7801\nif (!require(\"ggplot2\"))\n  install.packages('ggplot2') \nif (!require(\"vegan\"))\n  install.packages('vegan')\nif (!require(\"tidyverse\"))\n  install.packages('tidyverse') \nif (!require(\"BiocManager\"))\n  install.packages('BiocManager') \nif (!require(\"ComplexHeatmap\"))\n  BiocManager::install('ComplexHeatmap')\n# \u68c0\u67e5\u5f00\u53d1\u8005\u5de5\u5177devtools\uff0c\u5982\u6ca1\u6709\u5219\u5b89\u88c5\nif (!require(\"devtools\"))\n  install.packages(\"devtools\")\n# \u52a0\u8f7d\u5f00\u53d1\u8005\u5de5\u5177devtools\nlibrary(devtools)\n# \u68c0\u67e5linkET\u5305\uff0c\u6ca1\u6709\u5219\u901a\u8fc7github\u5b89\u88c5\u6700\u65b0\u7248\nif (!require(\"linkET\"))\n  install_github(\"Hy4m\/linkET\", force = TRUE)\n# \u52a0\u8f7d\u5305\nlibrary(ggplot2)\nlibrary(vegan)\nlibrary(linkET)\nlibrary(tidyverse)\nlibrary(ComplexHeatmap)\n#\u8bfb\u53d6\u6570\u636e\n#\u6839\u636e\u539f\u6587\u9644\u8868S1\uff0c\u83b7\u5f97\u6837\u54c1\u7684\u7269\u7406\u5316\u5b66\u7279\u6027\u548c\u5730\u7406\u5206\u5e03\u60c5\u51b5\uff0c\u4f5c\u4e3a\u73af\u5883\u56e0\u5b50\u8868\nenv&lt;-read.csv(\"env.CSV\",row.names = 1,header = T)\n#\u751f\u6210\u4e00\u4e2a\u7269\u79cd\u7ec4\u6210\u7684\u4e30\u5ea6\u8868\uff0c\u884c\u4e3a\u6837\u672c\uff0c\u5217\u4e3a\u7269\u79cd\uff0c\u884c\u6570\u5fc5\u987b\u4e0e\u73af\u5883\u56e0\u5b50\u8868\u7684\u884c\u6570\u76f8\u540c\nspec&lt;-read.csv(\"spec.CSV\",row.names = 1,header = T)\n\ncorM &lt;- cor(env,method = \"pearson\")#\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u77e9\u9635\n#\u56e0\u4e3a\u9700\u8981\u7ed8\u5236\u7684\u662f\u4e0a\u4e09\u89d2\u70ed\u56fe\uff0c\u9700\u5bf9\u77e9\u9635\u8fdb\u884c\u5904\u7406\uff0c\u4fdd\u7559\u5bf9\u89d2\u7ebf\u53ca\u4e00\u534a\u7684\u6570\u636e\u5373\u53ef\nncr&lt;-nrow(corM)\nfor (i in 1:ncr){\n  for (j in 1:ncr){\n    if (j>i){\n      corM&#91;i,j]&lt;-NA\n    }\n  }\n}\n\ncorM&lt;-as.data.frame(corM)%>%mutate(colID=rownames(corM))\n\n#\u5bbd\u8868\u8f6c\u957f\u8868\ncorM_long&lt;-pivot_longer(corM,cols=-\"colID\",names_to = \"env\",values_to=\"cor\")%>%na.omit()\n\n#\u6839\u636e\u539f\u56fe\uff0c\u56fa\u5b9ax\u8f74\u548cy\u8f74\u7684\u987a\u5e8f\ncorM_long$colID&lt;-factor(corM_long$colID,levels = rownames(corM))\ncorM_long$env&lt;-factor(corM_long$env,levels = rev(rownames(corM)))\n\nmantel&lt;-mantel_test(spec=spec,env = env)\n#spec\u53c2\u6570\u540e\u662f\u7269\u79cd\u7ec4\u6210\u6570\u636e\uff0cenv\u53c2\u6570\u540e\u662f\u73af\u5883\u56e0\u5b50\u6570\u636e\nn=nrow(corM)\ncurve_df&lt;-data.frame(\n  x0=rep(-1,n),\n  y0=rep(6,n),\n  x1=c(0:(n-1)-0.1),\n  y1=c(n:1)\n)%>%cbind(mantel)%>%\n  mutate(\n    line_col = ifelse(p &lt;= 0.001, '#D1C4E9', NA),  #\u6839\u636ep\u503c\u4e0d\u540c\u8bbe\u7f6e\u8fde\u63a5\u66f2\u7ebf\u7684\u989c\u8272\u4e0d\u540c\n    line_col = ifelse(p > 0.001 &amp; p &lt;= 0.01, '#607D8B', line_col),\n    line_col = ifelse(p > 0.01 &amp; p &lt;= 0.05, '#FF5722', line_col),\n    line_col = ifelse(p > 0.05, '#4CAF50', line_col),\n    linewidth = ifelse(r >= 0.4, 4, NA),\n    # \u6839\u636er\u503c\u4e0d\u540c\u8bbe\u7f6e\u8fde\u63a5\u66f2\u7ebf\u7684\u5bbd\u5ea6\u4e0d\u540c\n    linewidth = ifelse(r >= 0.2 &amp; r &lt; 0.4, 2, linewidth),\n    linewidth = ifelse(r &lt; 0.2, 0.8, linewidth)\n  )\n\npdf(\"Figure1A.pdf\",width = 10.04, height = 6.26)\ngrid.newpage()\n#\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a1\u884c1\u5217\u7684\u5e03\u5c40\npushViewport(viewport(layout = grid.layout(nrow = 1, ncol = 1)))\nvp_value &lt;- function(row, col){\n  viewport(layout.pos.row = row, layout.pos.col = col)\n} \np1&lt;-ggplot(corM_long)+\n  geom_tile(aes(colID,env),fill=\"white\",color=\"grey\")+ #\u6dfb\u52a0\u65b9\u6846\n  geom_point(aes(colID,env,size=abs(cor),fill=cor),color=\"black\",shape=21)+ #\u6dfb\u52a0\u6563\u70b9\n  scale_x_discrete(position = \"top\")+ #x\u8f74\u79fb\u52a8\u5230\u9876\u90e8\n  scale_y_discrete(position = \"right\")+ #y\u8f74\u79fb\u52a8\u5230\u53f3\u4fa7\n  scale_size_continuous(range = c(1,10))+\n  scale_fill_gradientn(colours=col_fun)+ #\u6e10\u53d8\u8272\u8bbe\u7f6e\n  theme(axis.ticks = element_blank(),\n        axis.title = element_blank(),\n        panel.background = element_blank())+ #\u53bb\u9664\u80cc\u666f\n  guides(size=\"none\",  #\u9690\u85cfsize\u56fe\u4f8b\n         fill=guide_colorbar(title=\"Pearson's r\"))+  #\u4fee\u6539fill\u56fe\u4f8b\u6807\u9898\n  geom_curve(data=curve_df,aes(x=x0,y=y0,xend=x1,yend=y1), #\u6839\u636e\u8d77\u70b9\u7ec8\u70b9\u7ed8\u5236\u5f27\u7ebf\n             size=curve_df$linewidth, #\u6839\u636er\u503c\u8bbe\u7f6e\u7ebf\u6761\u5bbd\u5ea6\n             color=curve_df$line_col,  #\u6839\u636ep\u503c\u8bbe\u7f6e\u7ebf\u6761\u989c\u8272\n             curvature = 0.2)+ #\u8bbe\u7f6e\u5f27\u5ea6\n  geom_point(data = curve_df,aes(x=x1,y=y1),size=3,color=\"red\") #\u6dfb\u52a0\u8fde\u7ebf\u7ec8\u70b9\u7684\u70b9\n#\u5c06\u56fep1\u6dfb\u52a0\u8fdb\u753b\u5e03\nprint(p1,vp = vp_value(row = 1, col = 1))\n\n#\u5229\u7528ComplexHeatmap\u7684Legend()\u51fd\u6570\u7ed8\u5236\u5355\u72ec\u7684\u56fe\u4f8b\n#\u7ed8\u5236Mantel's p\u56fe\u4f8b\nlgd1 = Legend(at = 1:4, legend_gp = gpar(fill = c('#D1C4E9','#607D8B','#FF5722','#4CAF50')), \n              title = \"Mantel's p\",\n              labels =c(\"\u2264 0.001\",\"0.001 - 0.01\",\"0.01 - 0.05\",\"> 0.05\"),\n              nr = 1)\n#\u7ed8\u5236Mantel's r\u56fe\u4f8b\nlgd2 = Legend(labels =c(\"&lt; 0.02\",\"0.02 - 0.04\",\"\u2265 0.04\"),\n              title = \"Mantel's r\",\n              #graphics\u53c2\u6570\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u9ad8\u5ea6\n              graphics = list(\n                function(x, y, w, h) grid.rect(x, y, w, h*0.1*0.8, gp = gpar(fill = \"black\")),\n                function(x, y, w, h) grid.rect(x, y, w, h*0.1*2, gp = gpar(fill = \"black\")),\n                function(x, y, w, h) grid.rect(x, y, w, h*0.1*4, gp = gpar(fill = \"black\"))\n              ),nr = 1)\nlgd = packLegend(lgd1, lgd2)\n\n#\u5c06\u56fe\u4f8b\u6dfb\u52a0\u8fdb\u76f8\u5173\u6027\u70ed\u56fe\u4e2d\ndraw(lgd, x = unit(45, \"mm\"), y = unit(5, \"mm\"), just = c( \"bottom\"))\n\ndev.off()\n#> quartz_off_screen \n#>                 2\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\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-731","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/731","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=731"}],"version-history":[{"count":1,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/731\/revisions"}],"predecessor-version":[{"id":737,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/731\/revisions\/737"}],"wp:attachment":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}