[R] Simple plot loop

Ben Neal bneal at spg.ucsd.edu
Fri May 4 08:33:24 CEST 2012


This is correct , now working. I did not have the preceeding comma in "Data2[,i] . . ."

Thanks very much! Very much appreciated. Ben Neal


-----Original Message-----
From: Peter Ehlers [mailto:ehlers at ucalgary.ca]
Sent: Thu 5/3/2012 3:04 PM
To: Ben Neal
Cc: Jim Lemon; r-help at r-project.org
Subject: Re: [R] Simple plot loop
 
Ben,

I think that your original for-loop would work if you just
replaced the 'i' in the lines() call with 'Data2[,i]':

    for (i in 2:length(Data2)) {
       lines(MONTH, Data2[, i], type="o", pch=22, lty=2, col="blue")
    }

Peter Ehlers

On 2012-05-03 07:04, Ben Neal wrote:
> Jim, thanks for the reply. I tried what you recommend, but I still get an error when running it, just as I did with the similar loops I was trying. Here is the error:
>
> Error in xy.coords(x, y) : 'x' and 'y' lengths differ
>
> That comes from this code:
>
> #Get file
> library(zoo)
> setwd("/Users/benjaminneal/Documents/All Panama/1110_Panama/CNAT_Segmenting")
> Data = read.csv("120503_CNAT_Summary.csv", header=T)
>
> #fill in missing data from last observation
> Data2<- na.locf(Data)
> attach(Data2)
>
> #Plot line, and make it loop
> for(column in names(Data2)[2:length(Data2)])
>    lines(MONTH,column,type="o",pch=22,lty=2,col="blue")
> ------------------------------------------------
> The problem perhaps is in my data. My columns are observations over time, column headers are individual names, and the first column is the time series in months. An example is here:
> MONTH   Tag101
> 0             234
> 2             236
> 4             239
> 8             300
> 10           320
>
> This then repeats for different individuals . . . I think my problem must be that my length of MONTH is different than my length of observations of each column . . . except that it is not, as far as I can tell! Thank you for assisting me with this simple but frustrating problem - I have the greatest respect for those of you who provide these answers that so many of us read and utilize. Thank you. Ben Neal
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org on behalf of Jim Lemon
> Sent: Thu 5/3/2012 3:40 AM
> To: Ben Neal
> Cc: r-help at r-project.org
> Subject: Re: [R] Simple plot loop
>
> On 05/03/2012 05:50 PM, Ben Neal wrote:
>> Trying to plot multiple lines from a simple matrix with headers (eight observations per column). I will be doing a number of these, with varying numbers of columns, and do not want to enter the header names for each one (I got frustrated and just wrote them out, which did work).
>>
>> Data reads fine, first plot is fine, but when i use the code at the bottom for a for i loop it tells me that x and y do not match. . .
>>
>> One other issue is that I would prefer not to specify the first column either, but when I enter Plot(MONTH, Data2[2] . . . it also does not plot. Should this not call the second column?
>>
>> Thank you very much for any comments. I know this is simple, but I appreciate any assistance. Cheers, Ben
>>
>> #########################################
>>
>> # LOAD DATA FROM CSV
>> library(zoo)
>> setwd("/Users/benjaminneal/Documents/All Panama/1110_Panama/CNAT_Segmenting")
>> Data = read.csv("120503_CNAT_Summary.csv", header=T)
>>
>> #fill in missing data from last observation
>> Data2<- na.locf(Data)
>> attach(Data2)
>>
>> # PLOT ALL ON ONE CHART
>> plot(MONTH,T102, type="o", ann="False", ylim=c(1, 100), pch=22, lty=2, col="red")
>> title(main="5m and 10 m Colpophylia natans colonies over time", ylab="% live coral / colony",
>>         xlab="Months", col.main="black", font.main=4)
>>
>> lines(MONTH,T162, type="o", pch=22, lty=2, col="red")
>> lines(MONTH,T231, type="o", pch=22, lty=2, col="green")
>> lines(MONTH,T250, type="o", pch=22, lty=2, col="green")
>>
>> ##(many other similar lines here, with entered column headers . . . up to 75)
>>
>> lines(MONTH,T373, type="o", pch=22, lty=2, col="blue")
>> lines(MONTH,T374, type="o", pch=22, lty=2, col="blue")
>> lines(MONTH,T377, type="o", pch=22, lty=2, col="blue")
>>
>> # Tried to add lines another way with for i loop, but this is the part not working
>> for (i in 2:length(Data2)) {
>>     lines(MONTH, i, type="o", pch=22, lty=2, col="blue"))
>> }
>> #####################################
>>
> Hi Ben,
> I think what you may want in your loop is this:
>
> for(column in names(Data2)[2:length(Data2)])
>    lines(MONTH,column,type="o",pch=22,lty=2,col="blue")
>
> But, if you want the first two lines to be green, you'll probably have
> to get a vector of colors:
>
> colorvec<-rep("blue",length(Data2))
> colorvec[1]<-"red"
> colorvec[2:3]<-"green"
>
> and change the above to:
>
> columnnames<-names(Data2)
> for(column in 2:length(Data2))
>    lines(MONTH,columnnames[column],type="o",pch=22,lty=2,col=colvec[column])
>
> Jim
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list