[R] Plot Many Data to same plot

Jim Lemon jim at bitwrit.com.au
Wed Feb 22 08:33:16 CET 2012

On 02/21/2012 11:56 PM, Alaios wrote:
> Dear all,
> I have a function that for a variable number of inputs plots them to the same plot
> I am doing this quite simply by
> plot(seq(from=start, to=stop, length.out=np), datalist[[1]]$dataset
>                   xlim=c(start, stop), ylim=c(0, 1), type="l")
>          if (length(datalist)>  1) {
>                  for (i in 2:length(datalist)) {
>                          np<- length(datalist[[i]]$dataset)
>                          lines(seq(from=start, to=stop, length.out=np), datalist[[i]]$dataset$, lty=i)
>                  }
>          }
> as you can see, specifically this line
>       lines(seq(from=start, to=stop, length.out=np), datalist[[i]]$dataset$ lty=i)
> is changing the line type so any different input is plotted with different line type.
> This works quite well for 6 lines but if the arguments are more than 6 (in my case 7) the line type starts from the beginning. Is it possible to keep that loop and have the lines produced in plots a bit more customized (like lines with squares and or cubes).
> I have already checked in the ?par
> but I can not find how I can modify the line in that sense, and especially doing this smart inside a for loop.
Hi Alex,
The easiest way is to define a vector of more line types and then step 
through that vector. Here is one with the default five broken lines and 
five more that are reasonably distinguishable:

mylinetypes=c("44", "13", "1343", "73", 
for(i in 1:10) abline(h=i,lty=mylinetypes[i])

Work out a way to generate different line types automatically? You might 
be able to do this by some tricky algorithm that applied a sequence like 
(+3,-2,+1,-1) to your index and then rotating the sequence on each 
iteration, BUT, your plot would still probably look like a mess with 
more than ten lines.


More information about the R-help mailing list