在同一图表上绘制特定行

时间:2014-09-29 23:48:09

标签: r

我使用该函数将数据框中的所有行绘制成pdf文件。

library(gplots)
# open the pdf file
pdf(file='Plots of all rows.pdf')

x <- 1:(ncol(tbl_alles)-1) 

for(i in 1:nrow(tbl_alles)){
  # plot onto a new pdf page 
  plot(x=x,y=tbl_alles[i,-1],type='b',main=tbl_alles[i,1],xlab='X',ylab='Y')
}

# close the pdf file
dev.off()

如何在不同颜色的同一图表上绘制3行?

作为图例使用行的名称......

我的数据:

> dput(head(tbl_alles))
structure(list(`10` = c(0, 0, 0, 0, 0, 0), `20` = c(0, 0, 0, 
0, 0, 0), `52.5` = c(0, 0, 0, 0, 0, 0), `81` = c(0, 0, 1, 0, 
0, 0), `110` = c(0, 0, 0, 0, 0, 0), `140.5` = c(0, 0, 0, 0, 0, 
0), `189` = c(0, 0, 0, 0, 0, 0), `222.5` = c(0, 0, 0, 0, 0, 0
), `278` = c(0, 0, 0, 0, 0, 0), `340` = c(0, 0, 0, 0, 0, 0), 
    `397` = c(0, 1, 0, 0, 0, 0), `453.5` = c(0, 0.66069369, 0, 
    0, 0, 1), `529` = c(0, 0.521435654, 0, 0, 1, 0), `580` = c(0, 
    0.437291195, 0, 0, 1, 0), `630.5` = c(0, 0.52204783, 0, 0, 
    0, 0), `683.5` = c(0, 0.52429838, 0, 0, 0, 0), `735.5` = c(1, 
    0.3768651, 0, 1, 0, 0), `784` = c(0, 0, 0, 0, 0, 0), `832` = c(0, 
    0, 0, 0, 0, 0), `882.5` = c(0, 0, 0, 0, 0, 0), `926.5` = c(0, 
    0, 0, 0, 0, 0), `973` = c(0, 0, 0, 0, 0, 0), `1108` = c(0, 
    0, 0, 0, 0, 0), `1200` = c(0, 0, 0, 0, 0, 0)), .Names = c("10", 
"20", "52.5", "81", "110", "140.5", "189", "222.5", "278", "340", 
"397", "453.5", "529", "580", "630.5", "683.5", "735.5", "784", 
"832", "882.5", "926.5", "973", "1108", "1200"), row.names = c("at1g01050.1", 
"at1g01080.1", "at1g01090.1", "at1g01220.1", "at1g01420.1", "at1g01470.1"
), class = "data.frame")

这是我希望看到的输出。只需将两个图表放在一些不同颜色的图表上即可。

Number 1

Number 2

编辑:

    > tbl_alles[1066,]
                10 20 52.5 81 110 140.5 189 222.5 278 340 397 453.5 529 580 630.5 683.5 735.5 784 832
    at3g01510.1  0  0    0  0   0     0   0     0   0   0   0     0   0   0     0     0     0   0   1
                882.5 926.5 973 1108 1200
    at3g01510.1     0     0   0    0    0

> tbl_alles[2269,]
            10 20 52.5 81 110 140.5 189 222.5 278 340 397 453.5 529 580 630.5 683.5 735.5 784 832
at5g26570.1  0  0    0  0   0     0   0     0   0   0   0     0   0   0     0     0     1   0   1
            882.5 926.5 973 1108 1200
at5g26570.1     0     0   0    0    0

> tbl_alles[109,]
            10 20 52.5 81 110 140.5 189 222.5 278 340 397 453.5 529 580 630.5     683.5     735.5
at1g10760.1  0  0    0  0   0     0   0     0   0   0   0     0   0   0     0 0.5707348 0.8569183
                  784 832 882.5 926.5 973 1108 1200
at1g10760.1 0.9070698   1     0     0   0    0    0

这就是我想要实现的目标:

Combined 3

1 个答案:

答案 0 :(得分:2)

我总是忘记matplot,我不熟悉gplot,所以我认为它很适合

这是我用来复制你的问题的一小部分

df<-structure(list(`10` = c(-0.371350351694367, -1.25087314646208, 
-0.657843532649542), `20` = c(-1.1851240454548, -2.78219491586262, 
0.710133251591709), `52.5` = c(1.68304915266843, -0.496047552485386, 
0.450948253352661), `81` = c(0.129735967979554, 0.254350517817915, 
-0.18288033694209), `110` = c(-1.13792416656106, 0.685305257987392, 
-0.00333217347885503), `140.5` = c(-3.60035706471287, 0.147934251860607, 
1.21899119774361), `189` = c(0.20579357437275, -0.287714362235557, 
-0.116174227557464), `222.5` = c(-2.27228706498774, 1.14330151676478, 
0.437320821753322), `278` = c(0.0184198982292088, 1.03280897369263, 
-0.809374548090546), `340` = c(1.10314005160547, -0.423635755487127, 
1.69263287858465), `397` = c(1.11000715987197, -0.666713965188873, 
-0.616331157049669), `453.5` = c(-0.21763982556254, -0.668405154120432, 
-0.101290244002727), `529` = c(-1.23612437632145, 0.596727546451954, 
1.65926804193271), `580` = c(-1.36914007441172, 0.553436632958187, 
0.370452646201169), `630.5` = c(-1.28739943545904, 1.51605377604701, 
0.535674548844182), `683.5` = c(0.0380431318762389, 0.259944835916881, 
-1.12267356731606), `735.5` = c(0.307116139352162, 0.619942543650423, 
1.43847332323359), `784` = c(0.517011770731407, -0.271348876993244, 
0.382706886840812), `832` = c(0.358756221875511, 0.902328658122764, 
2.19653579973421), `882.5` = c(0.60565816196684, -1.69443962691366, 
-0.338433483486653), `926.5` = c(2.15044754686289, -0.979574461038407, 
0.116260893315264), `973` = c(-1.3051680247044, -0.735063827396212, 
-1.55018820456708), `1108` = c(-0.108476761260576, 1.21094890415222, 
-1.04130290709525), `1200` = c(-0.963125050259433, -1.12921931676616, 
-0.357160373571803)), .Names = c("10", "20", "52.5", "81", "110", 
"140.5", "189", "222.5", "278", "340", "397", "453.5", "529", 
"580", "630.5", "683.5", "735.5", "784", "832", "882.5", "926.5", 
"973", "1108", "1200"), row.names = c("at1g01050.1", "at1g01080.1", 
"at1g01090.1"), class = "data.frame")

现在你的代码,请注意我使用matplot,转置df,将其转换为矩阵(对于matplot),并为我们将要绘制的每个数据集指定一个类型

library(gplots)
# open the pdf file
pdf(file='Plots of all rows.pdf')



rowsToPlot<-c(1,2,3)
# plot onto a new pdf page 
matplot(as.matrix(t(df[rowsToPlot,])),type=rep("l", length(rowsToPlot)), col=rainbow(length(rowsToPlot)))


# close the pdf file
dev.off()

我的照片

Graph