(转帖)-GO enrichment (Easier)

原文地址:https://mp.weixin.qq.com/s/cFX51WjlgS-SetbM8THqdg


首先还是讲一下配置文件的准备(这也是你唯一需要操心的地方了)。

这一次配置文件分为两个txt文档,分别存有上调和下调基因的GO富集结果,数据格式如下图所示:

左边是上调基因GO富集的结果,右边是下调基因GO富集的结果,每边1-4列分别是GO

terms, Gene number,

-log10(P-value)和fill(即第一行,作为列名),我是按的-log10(P-value)排序,选的top20的结果,这个可以自己定(但不要太多,比如top100。。。)(如果你想按照gene

number排序,只需要将第二列和第三列互换一下位置就好了,最后有演示)

颜色的选择建议使用http://colorbrewer2.org选择,具体使用方法如下:

在excel中编辑好文档后,将上下调基因GO富集结果分别复制粘贴到两个txt文档中,分别命名为up.txt,down.txt

保存好后,在同一目录下新建一个文本文档,将文末的代码复制粘贴到里面,保存,更改文件名为GO.R

然后右键,用R-studio打开,全选,Run

然后在同一目录下会生成一个GO.svg的文件,可以用Inkscape或者AI打开,直接另存为PDF即可。

然后,你就得到矢量图了

我是取得top20的,这个可以自己根据需要选择,比如我按基因数目排序选top15,换另外一种色系颜色,只需要修改配置文件就好了,不需要更改代码(知识点:配置文件是关键)。

R脚本:

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

# Author: Zhaodong Hao

# Create data: 2017-06-24

# Description: GO enrichment - 2

# Attention: you can use Inkscape or AI to open the output file (.svg) and save as a PDF file

# Contact: haozd1992@163.com

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

# set the working dictionary as the path of 'source'd file

whereFrom=sys.calls()[[1]]

# This should be an expression that looks something like

# source("pathname/myfilename.R")

whereFrom=as.character(whereFrom[2]) # get the pathname/filename

whereFrom=paste(getwd(),whereFrom,sep="/") # prefix it with the current working directory

pathnameIndex=gregexpr(".*/",whereFrom) # we want the string up to the final '/'

pathnameLength=attr(pathnameIndex[[1]],"match.length")

whereFrom=substr(whereFrom,1,pathnameLength-1)

setwd(whereFrom)

up_data_pre <- read.table("up.txt", sep = "\t")

up_data <- read.table("up.txt", sep = "\t", header = T)

colnames(up_data)

<- c(as.character(up_data_pre[1, 1]), as.character(up_data_pre[1,

2]), as.character(up_data_pre[1, 3]), as.character(up_data_pre[1, 4]))

down_data_pre <- read.table("down.txt", sep = "\t")

down_data <- read.table("down.txt", sep = "\t", header = T)

colnames(down_data)

<- c(as.character(down_data_pre[1, 1]),

as.character(down_data_pre[1, 2]), as.character(down_data_pre[1, 3]),

as.character(down_data_pre[1, 4]))

xlim_up_1 <-

ceiling(max(up_data[ ,2]) / (5 * 10 ^(nchar(ceiling(max(up_data[ ,2]))) -

2))) * (5 * 10 ^(nchar(ceiling(max(up_data[ ,2]))) - 2))

xlim_up_2

<- ceiling(max(up_data[ ,3]) / (5 * 10 ^(nchar(ceiling(max(up_data[

,3]))) - 2))) * (5 * 10 ^(nchar(ceiling(max(up_data[ ,3]))) - 2))

xlim_down_1

<- ceiling(max(down_data[ ,2]) / (5 * 10

^(nchar(ceiling(max(down_data[ ,2]))) - 2))) * (5 * 10

^(nchar(ceiling(max(down_data[ ,2]))) - 2))

xlim_down_2 <-

ceiling(max(down_data[ ,3]) / (5 * 10 ^(nchar(ceiling(max(down_data[

,3]))) - 2))) * (5 * 10 ^(nchar(ceiling(max(down_data[ ,3]))) - 2))

### rect model:

# x,y 矩形左上角点的坐标; rx,ry 圆角矩形;width,height 矩形的宽和高(从左上角的点向右向下延伸); fill 填充颜色;stroke 线的颜色;stroke-width 线的宽度;opacity 透明度

# up_data rect

for (i in 1:nrow(up_data)) {

  up_data[i, 5] <- 2.5 * 35.43307 + 1.5 * 35.43307 + (nrow(up_data[1:i,]) - 1) * (0.15 + 0.03) * 35.43307

}

names(up_data)[5] <- "y"

up_data$x <- (1.9 + 4 + 0.25) * 35.43307

for (i in 1:nrow(up_data)) {

  up_data[i, 7] <- up_data[i, 3] * (4 * 35.43307) / xlim_up_2

}

names(up_data)[7] <- "width"

up_data$rect

= paste("

width=\"", up_data$width,  "\" height=\"", 0.15 * 35.43307, "\"

style=\"fill:#", up_data$fill, "\"/>", sep = "")

# down_data rect

for (i in 1:nrow(down_data)) {

  down_data[i, 5] <- 2.5 * 35.43307 + 1.5 * 35.43307 + (nrow(down_data[1:i,]) - 1) * (0.15 + 0.03) * 35.43307

}

names(down_data)[5] <- "y"

for (i in 1:nrow(down_data)) {

  down_data[i, 6] <- (1.9 + 4 + 0.25 + 4 + 0.7 + 4) * 35.43307 - down_data[i, 3] * (4 * 35.43307) / xlim_down_2

}

names(down_data)[6] <- "x"

for (i in 1:nrow(down_data)) {

  down_data[i, 7] <- down_data[i, 3] * (4 * 35.43307) / xlim_down_2

}

names(down_data)[7] <- "width"

down_data$rect

= paste("

width=\"", down_data$width,  "\" height=\"", 0.15 * 35.43307, "\"

style=\"fill:#", down_data$fill, "\"/>", sep = "")

### polyline model:

# up_data polyline

for (i in 1:nrow(up_data)) {

  up_data[i, 9] = (1.9 + 4 + 0.25) * 35.43307 + up_data[i, 2] * (4 * 35.43307) / xlim_up_1

}

names(up_data)[9] <- "x1"

for (i in 1:nrow(up_data)) {

  up_data[i, 10] = 2.5 * 35.43307 + 1.5 * 35.43307 + 0.075 * 35.43307 + (nrow(up_data[1:i,]) - 1) * (0.15 + 0.03) * 35.43307

}

names(up_data)[10] <- "y1"

polyline_1

<- paste("

up_data$y1, sep = ""), collapse = " "), "\"

style=\"fill:white;stroke:black;stroke-width:0.8;fill-opacity:0\"/>")

# down_data polyline

for (i in 1:nrow(down_data)) {

  down_data[i, 9] = (1.9 + 4 + 0.25 + 4 + 0.7 + 4) * 35.43307 - down_data[i, 2] * (4 * 35.43307) / xlim_down_1

}

names(down_data)[9] <- "x1"

for (i in 1:nrow(down_data)) {

  down_data[i, 10] = 2.5 * 35.43307 + 1.5 * 35.43307 + 0.075 * 35.43307 + (nrow(down_data[1:i,]) - 1) * (0.15 + 0.03) * 35.43307

}

names(down_data)[10] <- "y1"

polyline_2

<- paste("

down_data$y1, sep = ""), collapse = " "), "\"

style=\"fill:white;stroke:black;stroke-width:0.8;fill-opacity:0\"/>")

polyline_left_1

<- paste("

+ max(up_data$width[1], up_data[1, 2] * (4 * 35.43307) / xlim_up_1) +

1/5 * (4 * 35.43307 - max(up_data$width[1], up_data[1, 2] * (4 *

35.43307) / xlim_up_1)), up_data$y1[1], sep = ","),

                         paste((1.9 + 4 + 0.25 + 4) * 35.43307, up_data$y1[1], sep = ","), 

                         paste((1.9 + 4 + 0.25 + 4) * 35.43307, up_data$y1[nrow(up_data)/3], sep = ","), 


                       paste((1.9 + 4 + 0.25) * 35.43307 +

max(up_data$width[nrow(up_data)/3], up_data[nrow(up_data)/3, 2] * (4 *

35.43307) / xlim_up_1) + 1/5 * (4 * 35.43307 -

max(up_data$width[nrow(up_data)/3], up_data[nrow(up_data)/3, 2] * (4 *

35.43307) / xlim_up_1)), up_data$y1[nrow(up_data)/3], sep = ","),

                         "\" style=\"fill:white;stroke:black;stroke-width:0.5;fill-opacity:0\"/>")

polyline_left_2

<- paste("

+ max(up_data$width[nrow(up_data)/3 + 1], up_data[nrow(up_data)/3 + 1,

2] * (4 * 35.43307) / xlim_up_1) + 1/5 * (4 * 35.43307 -

max(up_data$width[nrow(up_data)/3 + 1], up_data[nrow(up_data)/3 + 1, 2] *

(4 * 35.43307) / xlim_up_1)), up_data$y1[nrow(up_data)/3 + 1], sep =

","),

                         paste((1.9 + 4 + 0.25 + 4) * 35.43307, up_data$y1[nrow(up_data)/3 + 1], sep = ","), 

                         paste((1.9 + 4 + 0.25 + 4) * 35.43307, up_data$y1[nrow(up_data)/3 * 2], sep = ","), 


                       paste((1.9 + 4 + 0.25) * 35.43307 +

max(up_data$width[nrow(up_data)/3 * 2], up_data[nrow(up_data)/3 * 2, 2] *

(4 * 35.43307) / xlim_up_1) + 1/5 * (4 * 35.43307 -

max(up_data$width[nrow(up_data)/3 * 2], up_data[nrow(up_data)/3 * 2, 2] *

(4 * 35.43307) / xlim_up_1)), up_data$y1[nrow(up_data)/3 * 2], sep =

","),

                         "\" style=\"fill:white;stroke:black;stroke-width:0.5;fill-opacity:0\"/>")

polyline_left_3

<- paste("

+ max(up_data$width[nrow(up_data)/3 * 2 + 1], up_data[nrow(up_data)/3 *

2 + 1, 2] * (4 * 35.43307) / xlim_up_1) + 1/5 * (4 * 35.43307 -

max(up_data$width[nrow(up_data)/3 * 2 + 1], up_data[nrow(up_data)/3 * 2 +

1, 2] * (4 * 35.43307) / xlim_up_1)), up_data$y1[nrow(up_data)/3 * 2 +

1], sep = ","),

                         paste((1.9 + 4 + 0.25 + 4) * 35.43307, up_data$y1[nrow(up_data)/3 * 2 + 1], sep = ","), 

                         paste((1.9 + 4 + 0.25 + 4) * 35.43307, up_data$y1[nrow(up_data)/3 * 3], sep = ","), 


                       paste((1.9 + 4 + 0.25) * 35.43307 +

max(up_data$width[nrow(up_data)/3 * 3], up_data[nrow(up_data)/3 * 3, 2] *

(4 * 35.43307) / xlim_up_1) + 1/5 * (4 * 35.43307 -

max(up_data$width[nrow(up_data)/3 * 3], up_data[nrow(up_data)/3 * 3, 2] *

(4 * 35.43307) / xlim_up_1)), up_data$y1[nrow(up_data)/3 * 3], sep =

","),

                         "\" style=\"fill:white;stroke:black;stroke-width:0.5;fill-opacity:0\"/>")

polyline_right_1

<- paste("

35.43307 + 4/5 * (4 * 35.43307 - max(down_data$width[1], down_data[1,

2] * (4 * 35.43307) / xlim_down_1)), down_data$y1[1], sep = ","),

                          paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, down_data$y1[1], sep = ","), 

                          paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, down_data$y1[nrow(down_data)/3], sep = ","), 


                        paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307 +

4/5 * (4 * 35.43307 - max(down_data$width[nrow(down_data)/3],

down_data[nrow(down_data)/3, 2] * (4 * 35.43307) / xlim_down_1)),

down_data$y1[nrow(down_data)/3], sep = ","),

                          "\" style=\"fill:white;stroke:black;stroke-width:0.5;fill-opacity:0\"/>")

polyline_right_2

<- paste("

35.43307 + 4/5 * (4 * 35.43307 - max(down_data$width[nrow(up_data)/3 +

1], down_data[nrow(up_data)/3 + 1, 2] * (4 * 35.43307) / xlim_down_1)),

down_data$y1[nrow(up_data)/3 + 1], sep = ","),

                          paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, down_data$y1[nrow(up_data)/3 + 1], sep = ","), 

                          paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, down_data$y1[nrow(down_data)/3 * 2], sep = ","), 


                        paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307 +

4/5 * (4 * 35.43307 - max(down_data$width[nrow(down_data)/3 * 2],

down_data[nrow(down_data)/3 * 2, 2] * (4 * 35.43307) / xlim_down_1)),

down_data$y1[nrow(down_data)/3 * 2], sep = ","),

                          "\" style=\"fill:white;stroke:black;stroke-width:0.5;fill-opacity:0\"/>")

polyline_right_3

<- paste("

35.43307 + 4/5 * (4 * 35.43307 - max(down_data$width[nrow(up_data)/3 * 2

+ 1], down_data[nrow(up_data)/3 * 2 + 1, 2] * (4 * 35.43307) /

xlim_down_1)), down_data$y1[nrow(up_data)/3 * 2 + 1], sep = ","),

                          paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, down_data$y1[nrow(up_data)/3 * 2 + 1], sep = ","), 

                          paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, down_data$y1[nrow(down_data)/3 * 3], sep = ","), 


                        paste((1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307 +

4/5 * (4 * 35.43307 - max(down_data$width[nrow(down_data)/3 * 3],

down_data[nrow(down_data)/3 * 3, 2] * (4 * 35.43307) / xlim_down_1)),

down_data$y1[nrow(down_data)/3 * 3], sep = ","),

                          "\" style=\"fill:white;stroke:black;stroke-width:0.5;fill-opacity:0\"/>")

### line model:

# up_data y-ticks

up_data$Y_ticks

<- paste("

x2=\"", up_data$x - 0.15 * 35.43307, "\" y2=\"", up_data$y1, "\"

style=\"stroke:black;stroke-width:0.5\"/>", sep = "")

# down_data y-ticks

down_data$Y_ticks

<- paste("

y1=\"", down_data$y1, "\" x2=\"", (17.2 + 1.9 - 4 - 0.1) * 35.43307, "\"

y2=\"", down_data$y1, "\"

style=\"stroke:black;stroke-width:0.5\"/>", sep = "")

# up_data x & -ticks_bottom

up_data_x_bottom

<- paste("

1.5 * 35.43307 + nrow(up_data) * (0.15 + 0.03) * 35.43307 + 0.03 *

35.43307, "\" x2=\"", 217.9134 + 4 * 35.43307, "\" y2=\"", 2.5 *

35.43307 + 1.5 * 35.43307 + nrow(up_data) * (0.15 + 0.03) * 35.43307 +

0.03 * 35.43307, "\" style=\"stroke:black;stroke-width:1\"/>", sep =

"")

up_data_x_ticks_bottom <- data.frame(seq(0, xlim_up_2, xlim_up_2 / 5))

up_data_x_ticks_bottom$ticks

<- paste("

up_data_x_ticks_bottom[,1] * (4 * 35.43307) / xlim_up_2, "\" y1=\"", 2.5

* 35.43307 + 1.5 * 35.43307 + nrow(up_data) * (0.15 + 0.03) * 35.43307 +

0.03 * 35.43307, "\" x2=\"", (1.9 + 4 + 0.25) * 35.43307 +

up_data_x_ticks_bottom[,1] * (4 * 35.43307) / xlim_up_2, "\" y2=\"", 2.5

* 35.43307 + 1.5 * 35.43307 + nrow(up_data) * (0.15 + 0.03) * 35.43307 +

0.15 * 35.43307 + 0.03 * 35.43307, "\"

style=\"stroke:black;stroke-width:0.5\"/>", sep = "")

# down_data x & -ticks_bottom

down_data_x_bottom

<- paste("

1.5 * 35.43307 + nrow(down_data) * (0.15 + 0.03) * 35.43307 + 0.03 *

35.43307, "\" x2=\"", 384.4488 + 4 * 35.43307, "\" y2=\"", 2.5 *

35.43307 + 1.5 * 35.43307 + nrow(down_data) * (0.15 + 0.03) * 35.43307 +

0.03 * 35.43307, "\" style=\"stroke:black;stroke-width:1\"/>", sep =

"")

down_data_x_ticks_bottom <- data.frame(seq(0, xlim_down_2, xlim_down_2 / 5))

down_data_x_ticks_bottom$ticks

<- paste("

down_data_x_ticks_bottom[,1] * (4 * 35.43307) / xlim_down_2, "\" y1=\"",

2.5 * 35.43307 + 1.5 * 35.43307 + nrow(down_data) * (0.15 + 0.03) *

35.43307 + 0.03 * 35.43307, "\" x2=\"", (17.2 + 1.9 - 4 - 0.25) *

35.43307 - down_data_x_ticks_bottom[,1] * (4 * 35.43307) / xlim_down_2,

"\" y2=\"", 2.5 * 35.43307 + 1.5 * 35.43307 + nrow(down_data) * (0.15 +

0.03) * 35.43307 + 0.15 * 35.43307 + 0.03 * 35.43307, "\"

style=\"stroke:black;stroke-width:0.5\"/>", sep = "")

# up_data x & -ticks_top

up_data_x_top

<- paste("

1.5 * 35.43307 - (0.03 + 0.03) * 35.43307, "\" x2=\"", 217.9134 + 4 *

35.43307, "\" y2=\"", 2.5 * 35.43307 + 1.5 * 35.43307 - (0.03 + 0.03) *

35.43307, "\" style=\"stroke:black;stroke-width:1\"/>", sep = "")

up_data_x_ticks_top <- data.frame(seq(0, xlim_up_1, xlim_up_1 / 5))

up_data_x_ticks_top$ticks

<- paste("

up_data_x_ticks_top[,1] * (4 * 35.43307) / xlim_up_1, "\" y1=\"", 2.5 *

35.43307 + 1.5 * 35.43307 - (0.03 + 0.03) * 35.43307, "\" x2=\"", (1.9 +

4 + 0.25) * 35.43307 + up_data_x_ticks_top[,1] * (4 * 35.43307) /

xlim_up_1, "\" y2=\"", 2.5 * 35.43307 + 1.5 * 35.43307 - (0.03 + 0.03) *

35.43307 - 0.15 * 35.43307, "\"

style=\"stroke:black;stroke-width:0.5\"/>", sep = "")

# down_data x & -ticks_top

down_data_x_top

<- paste("

1.5 * 35.43307 - (0.03 + 0.03) * 35.43307, "\" x2=\"", 384.4488 + 4 *

35.43307, "\" y2=\"", 2.5 * 35.43307 + 1.5 * 35.43307 - (0.03 + 0.03) *

35.43307, "\" style=\"stroke:black;stroke-width:1\"/>", sep = "")

down_data_x_ticks_top <- data.frame(seq(0, xlim_down_1, xlim_down_1 / 5))

down_data_x_ticks_top$ticks

<- paste("

down_data_x_ticks_top[,1] * (4 * 35.43307) / xlim_down_1, "\" y1=\"",

2.5 * 35.43307 + 1.5 * 35.43307 - (0.03 + 0.03) * 35.43307, "\" x2=\"",

(17.2 + 1.9 - 4 - 0.25) * 35.43307 - down_data_x_ticks_top[,1] * (4 *

35.43307) / xlim_down_1, "\" y2=\"", 2.5 * 35.43307 + 1.5 * 35.43307 -

(0.03 + 0.03) * 35.43307 - 0.15 * 35.43307, "\"

style=\"stroke:black;stroke-width:0.5\"/>", sep = "")

### text model

up_data_x_ticks_bottom$text

<- paste("

up_data_x_ticks_bottom[,1] * (4 * 35.43307) / xlim_up_2 -

nchar(up_data_x_ticks_bottom[,1]) * 2, "\" y=\"", 2.5 * 35.43307 + 1.5 *

35.43307 + nrow(up_data) * (0.15 + 0.03) * 35.43307 + 0.03 * 35.43307 +

0.5 * 35.43307, "\" font-family=\"Arial\" font-size=\"8\"

fill=\"black\" >", up_data_x_ticks_bottom[,1], "", sep =

"")

down_data_x_ticks_bottom$text <- paste("

(17.2 + 1.9 - 4 - 0.25) * 35.43307 - down_data_x_ticks_bottom[,1] * (4 *

35.43307) / xlim_down_2 - nchar(down_data_x_ticks_bottom[,1]) * 2, "\"

y=\"", 2.5 * 35.43307 + 1.5 * 35.43307 + nrow(down_data) * (0.15 + 0.03)

* 35.43307 + 0.03 * 35.43307 + 0.5 * 35.43307, "\"

font-family=\"Arial\" font-size=\"8\" fill=\"black\" >",

down_data_x_ticks_bottom[,1], "", sep = "")

up_data_x_ticks_top$text

<- paste("

up_data_x_ticks_top[,1] * (4 * 35.43307) / xlim_up_1 -

nchar(up_data_x_ticks_top[,1]) * 2, "\" y=\"", 2.5 * 35.43307 + 1.5 *

35.43307 - (0.03 + 0.03) * 35.43307 - 0.25 * 35.43307, "\"

font-family=\"Arial\" font-size=\"8\" fill=\"black\" >",

up_data_x_ticks_top[,1], "", sep = "")

down_data_x_ticks_top$text

<- paste("

down_data_x_ticks_top[,1] * (4 * 35.43307) / xlim_down_1 -

nchar(down_data_x_ticks_top[,1]) * 2, "\" y=\"", 2.5 * 35.43307 + 1.5 *

35.43307 - (0.03 + 0.03) * 35.43307 - 0.25 * 35.43307, "\"

font-family=\"Arial\" font-size=\"8\" fill=\"black\" >",

down_data_x_ticks_top[,1], "", sep = "")

up_data$text

<- paste("

up_data$y1 + 0.075 * 35.43307, "\" text-anchor=\"end\"

font-family=\"Arial\" font-size=\"6\" fill=\"black\" >", up_data[

,1], "", sep = "")

down_data$text <-

paste("

down_data$y1 + 0.075 * 35.43307, "\" font-family=\"Arial\"

font-size=\"6\" fill=\"black\" >", down_data[ ,1], "",

sep = "")

labels <- data.frame(paste("

0.25 + 4) * 35.43307, "\" y=\"", 2.5 * 35.43307 + 1.5 * 35.43307 +

nrow(up_data) * (0.15 + 0.03) * 35.43307 + 0.03 * 35.43307 + 1 *

35.43307, "\" text-anchor=\"end\" font-family=\"Arial\" font-size=\"8\"

fill=\"black\" >", colnames(up_data)[3], "", sep = ""),


                   paste("

35.43307, "\" y=\"", 2.5 * 35.43307 + 1.5 * 35.43307 + nrow(up_data) *

(0.15 + 0.03) * 35.43307 + 0.03 * 35.43307 + 1 * 35.43307, "\"

font-family=\"Arial\" font-size=\"8\" fill=\"black\" >",

colnames(down_data)[3], "", sep = ""),


     paste("

2.5 * 35.43307 + 1.5 * 35.43307 - (0.03 + 0.03) * 35.43307 - 0.25 *

35.43307 - 0.5 * 35.43307, "\" text-anchor=\"end\" font-family=\"Arial\"

font-size=\"8\" fill=\"black\" >", colnames(up_data)[2],

"", sep = ""),

                     paste("

x=\"", (1.9 + 4 + 0.25 + 4 + 0.7) * 35.43307, "\" y=\"", 2.5 * 35.43307 +

1.5 * 35.43307 - (0.03 + 0.03) * 35.43307 - 0.25 * 35.43307 - 0.5 *

35.43307, "\" font-family=\"Arial\" font-size=\"8\" fill=\"black\"

>", colnames(down_data)[2], "", sep = "")

)

BP

<- paste("

"\" y=\"", (up_data$y1[nrow(up_data)/3] - up_data$y1[1])/2 +

up_data$y1[1] + 0.075 * 35.43307, "\" text-anchor=\"middle\"

font-family=\"Arial\" font-size=\"8\" fill=\"black\"

>BP", sep = "")

CC <- paste("

(1.9 + 4 + 0.25 + 4 + 0.35) * 35.43307, "\" y=\"",

(up_data$y1[nrow(up_data)/3 * 2] - up_data$y1[nrow(up_data)/3])/2 +

up_data$y1[nrow(up_data)/3] + 0.075 * 35.43307, "\"

text-anchor=\"middle\" font-family=\"Arial\" font-size=\"8\"

fill=\"black\" >CC", sep = "")

MF <-

paste("

y=\"", (up_data$y1[nrow(up_data)/3 * 3] - up_data$y1[nrow(up_data)/3 *

2])/2 + up_data$y1[nrow(up_data)/3 * 2] + 0.075 * 35.43307, "\"

text-anchor=\"middle\" font-family=\"Arial\" font-size=\"8\"

fill=\"black\" >MF", sep = "")

# write .svg file

first_line <- data.frame("",

                         "

                         "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">",

                         "",

                         paste("", "\t")

)

write.table(first_line[1, 1], "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE)

write.table(first_line[1, 2], "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(first_line[1, 3], "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(first_line[1, 4], "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(first_line[1, 5], "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data$rect, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data$rect, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_1, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_2, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_left_1, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_left_2, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_left_3, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_right_1, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_right_2, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(polyline_right_3, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data$Y_ticks, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data$Y_ticks, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data_x_bottom, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data_x_ticks_bottom$ticks, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data_x_bottom, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data_x_ticks_bottom$ticks, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data_x_top, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data_x_ticks_top$ticks, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data_x_top, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data_x_ticks_top$ticks, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data_x_ticks_bottom$text, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data_x_ticks_bottom$text, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data_x_ticks_top$text, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data_x_ticks_top$text, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(up_data$text, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(down_data$text, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(labels, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(BP, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(CC, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

write.table(MF, "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

last_line <- data.frame(paste(""))

write.table(last_line[1, 1], "GO.svg", col.names = FALSE, row.names = FALSE, quote = FALSE, append = TRUE)

脚本比较长,不过你们完全不需要看,只要复制粘贴一下就OK啦😄

P.S. 想看懂代码的可以先学一学基础的R处理数据和SVG脚本(http://tutorials.jenkov.com/svg/index.html)。另外,百度云里面我放了R脚本和示例文件哦(链接: https://pan.baidu.com/s/1geLSbpL 密码: b7wn)。目前我测试了几次数据,只要配置文件没问题应该就没问题了,后期有问题的可以发我邮箱(haozd1992@163.com),欢迎提问,共同进步!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容