{"id":318,"date":"2018-04-11T16:40:59","date_gmt":"2018-04-11T08:40:59","guid":{"rendered":"http:\/\/www.wuchangsong.com\/?p=318"},"modified":"2018-04-11T16:40:59","modified_gmt":"2018-04-11T08:40:59","slug":"pca%e5%9b%be%e7%bb%98%e5%88%b6","status":"publish","type":"post","link":"http:\/\/www.wuchangsong.com\/?p=318","title":{"rendered":"PCA\u56fe\u7ed8\u5236"},"content":{"rendered":"<p>\u8f93\u5165\u8868\u8fbe\u77e9\u9635\u6570\u636e\u6587\u4ef6<\/p>\n<pre>\r\nlibrary(ggplot2)\r\nlibrary(gmodels)\r\ninname = \"COS_deseq_counts_normalized.txt\"\r\noutname = \"COS_PCA.png\"\r\ngroup <- factor(c(rep(\"COS0\",3),rep(\"COS1\",3),rep(\"COS3\",3),rep(\"COS6\",3),rep(\"COS12\",3)), levels = c(\"COS0\", \"COS1\",\"COS3\",\"COS6\",\"COS12\"))\r\n## step 1: \u6570\u636e\u7684\u8bfb\u53d6\u548c\u5904\u7406\r\n# read the expr data\r\nexpr <- read.table(inname, header=T, row.names=1)\r\n# transpose the data\r\ndata <- t(expr)\r\n## step2\uff1aPCA\u5206\u6790\r\n# do PCA \r\ndata.pca <- fast.prcomp(data)\r\n## step3\uff1a PCA\u7ed3\u679c\u89e3\u6790\r\n# fetch the proportion of PC1 and PC2\r\n# \u4e00\u822c\u60c5\u51b5\u4e0bPC1 + PC2 > 70% \u4e8c\u7ef4PCA\u6563\u70b9\u56fe\u624d\u6709\u6548\r\na <- summary(data.pca)\r\ntmp <- a[4]$importance\r\npro1 <- as.numeric(sprintf(\"%.3f\",tmp[2,1]))*100\r\npro2 <- as.numeric(sprintf(\"%.3f\",tmp[2,2]))*100\r\n\r\n# \u5c06\u6210\u5206\u77e9\u9635\u8f6c\u6362\u4e3a\u6570\u636e\u6846\r\npc = as.data.frame(a$x)\r\n\r\n# \u7ed9pc\u7684\u6570\u636e\u6846\u6dfb\u52a0\u540d\u79f0\u5217\u548c\u5206\u7ec4\u5217\uff08\u7528\u6765\u753b\u56fe\uff09\r\npc$group = group\r\npc$names = rownames(pc)\r\n\r\n## step 4: \u7ed8\u56fe\r\n# draw PCA plot figure\r\nxlab=paste(\"PC1(\",pro1,\"%)\",sep=\"\") \r\nylab=paste(\"PC2(\",pro2,\"%)\",sep=\"\")\r\npca=ggplot(pc,aes(PC1,PC2)) + \r\ngeom_point(size=3,aes(shape=group,color=group)) + \r\ngeom_text(aes(label=names),size=4)+labs(x=xlab,y=ylab,title=\"PCA\") + \r\ngeom_hline(yintercept=0,linetype=4,color=\"grey\") + \r\ngeom_vline(xintercept=0,linetype=4,color=\"grey\") + \r\ntheme_bw()\r\n\r\n# \u4fdd\u5b58\u7ed3\u679c\r\nggsave(outname,pca,width=10,height=8)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8f93\u5165\u8868\u8fbe\u77e9\u9635\u6570\u636e\u6587\u4ef6 library(ggplot2) library(gmodels) inname = &#8220;COS_deseq_counts_normalized.txt&#8221; outname = &#8220;COS_PCA.png&#8221; group<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts\/318"}],"collection":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":1,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":319,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/319"}],"wp:attachment":[{"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wuchangsong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}