-1

This code will show several plots on one window. As each plot was produced according to each column named X1 to X13 . SO I want to add as a main to each figure the name of the column used to plot the figure see below to distinguish which is whichenter 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')
   }
   )
Jonsson Sali
  • 73
  • 1
  • 10
  • 3
    Can't you just add `main = paste( column )` inside your `plot()` command? – Simon O'Hanlon Mar 13 '13 at 18:54
  • 3
    for curiosity's sake, you may want to post some dummy data and you'll get exposed to the ggplot2 / lattice way of dealing with such tasks. Warning: there's no turning back. – baptiste Mar 13 '13 at 19:11
  • In addition to what @baptiste said, you really will want to have a look at `ggplot2`, in particular `facet_wrap` and `facet_grid`. Facetting is `ggplot2` jargon for creating a grid of plots, each for a level of a factor (categorical variable). – Paul Hiemstra Mar 13 '13 at 21:13

2 Answers2

6

I'm sure you can adapt this into your code, but it's as simple as adding main = paste(column) inside your plot command.

An example:

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

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
3

Wouldn't this just need to add this line inside that function, perhaps after the points call:

title(main=column)
IRTFM
  • 258,963
  • 21
  • 364
  • 487