如何在R中为每个图添加标题?

时间:2013-03-13 18:49:12

标签: r plot

此代码将在一个窗口中显示几个图。因为每个图是根据名为X1到X13的每个列产生的。所以我想在每个图中添加as a main用于绘制图形的列的名称,如下所示,以区分哪个是enter image description here

ref= read.table("D:\\AS_asc.txt", sep="",header=TRUE)
sour1 = read.table("D:\\re.txt", sep="",header=TRUE) 
sour2= read.table("D:\\_asc.txt", sep="",header=TRUE)
columns <- paste0("X", 1:13)
par(mfrow=c(4,4))
 lapply(
 columns,
 function(column)
  {
   result1 <- (
    mean(ref[[column]]) - 
     ((sd(ref[[column]]) / sd(sour1[[column]])) * mean(sour1[[column]])) + 
     ((sd(ref[[column]]) / sd(sour1[[column]]) * sour1[[column]]))
     )   # calculate using ref and sour1
     result2 <- ((  
      mean(ref[[column]]) - 
      ((sd(ref[[column]]) / sd(sour2[[column]], na.rm=TRUE)) * mean(sour2[[column]], na.rm=TRUE)) + 
     ((sd(ref[[column]]) / sd(sour2[[column]], na.rm=TRUE) * sour2[[column]])) 
    ))  # calculate using ref and sour2
      plot(
    ref[[column]],
    result1,
    ylab = "[[column]]",
    xlab = "[[column]]",
   col  = 2
    )

   points(ref[[column]], ref[[column]], col = 'green')
  points(ref[[column]], result2, col = 'blue')
   }
   )

2 个答案:

答案 0 :(得分:6)

我确信你可以将它调整到你的代码中,但它就像在plot命令中添加main = paste(column)一样简单。

一个例子:

columns <- paste0( "X" , 1:9 )
par(mfrow=c(3,3))
lapply( columns , function(column){ plot(1:10 , y = 1:10 , main = paste(column ) ) } )

enter image description here

答案 1 :(得分:3)

这可能不需要在该函数中添加此行,可能是在points调用之后:

title(main=column)