[R] Bin Category Labels on Axis

jproville jeremy.proville at mail.mcgill.ca
Wed Jun 17 00:13:33 CEST 2009


Thanks David! I had trouble understanding how to convert factors, and was
playing around with as.numeric but it had never occurred to me to use a
combination of both that and as.character. 
I am getting really close to the graph I want - I've played around with the
arguments however I am still hitting a snag, if you have any insight as to
how to solve this I would be really grateful:

There seems to be some sort of difficulty in labeling axes spanning from a
negative integer to a positive - I am getting the following error message
Error in `[.default`(z$bins, seq(-36, 33, by = 5)) : 
  only 0's may be mixed with negative subscripts

Thanks again for your help! 

Jeremy Proville
M.Sc. Candidate 
Bioresource Engineering, McGill University




David Winsemius wrote:
> 
> The canonical method of creating a numeric value from a factor is to  
> wrap the factor in as.numeric(as.character( (.))  The as.character  
> grabs the value from the factor levels rahter than giving you the  
> internal codings and the as.numeic finishes the job.
> 
> Set xaxt=FALSE in the plot arguments. The axis can be managed with the  
> axis() function with at= and labels= arguments. You might want to not  
> plot every one of them, perhaps:
> 
>   at = seq(-36, 33, by = 5) , labels = as.numeric(as.factor(z 
> $bins[seq(-36, 33, by = 5)] )),
> -- 
> David Winsemius
> 
> On Jun 15, 2009, at 7:09 PM, jproville wrote:
> 
>>
>> Hi,
>>
>> I'd really appreciate if someone could give me some help or advice  
>> about
>> this - I've tried everything I know and am clueless about how to  
>> proceed!
>>
>> I've written a script to import ASCII data of raster maps, bin them  
>> into
>> categories, calculate the mean values within these bins and plot the  
>> two in
>> a simple graph. I'm running into problems with my x axis, as R  
>> cannot add
>> the bin categories as labels and simply outputs the factor values of  
>> the
>> number of bins.
>>
>> In much more detail:
>> One set of data is fractional values for the amount of cropland in a
>> designated area (raster cell). The second is annual average  
>> temperature
>> data, in the same format. I import these using:
>> xRaw<-scan(".../Temp.txt")
>> yRaw<-scan(".../Cropland.txt")
>>
>> and then designate cells with values of -9999 as NAs:
>> x<-ifelse(xRaw==-9999,NA,xRaw)
>> y<-ifelse(yRaw==-9999,NA,yRaw)
>>
>> Then, I compile them into a 2 column data frame, and exclude the NA  
>> values:
>> zRaw<-data.frame(x,y)
>> z<-na.exclude(zRaw)
>>
>> I am then left with data frame 'z' which is a refined list of data.  
>> Using
>> the 'cut' function, I assign each row into one of 69 different bins:
>> z$bins<-cut(z$x, breaks=c(-36:33), include.lowest=TRUE)
>>
>> Each bin is now 1 degree Celsius wide. Within each one, I calculate  
>> the mean
>> value of the corresponding fractional cropland data:
>> a<-tapply(z$y, z$bins, mean)
>>
>> Object 'a' now contains each bin category and the associated mean  
>> cropland
>> values, so 69 points in total.
>>
>>
>> Now, a problem arises when I try and plot these values using:
>> plot(a, xlab="Temperature", ylab="Fraction of Cropland", pch=20)
>> What happens is that R intuitively labels my x-axis from 1-70, which
>> corresponds to the bin factor values that were returned when the 'cut'
>> function was used. I would like to simply label the x-axis with my
>> temperature values, from -36 degrees to 33 degrees, instead of having
>> numbers 1-69. How can I do this??
>>
>> Thanks,
>>
>> Jeremy Proville
>> M.Sc. Candidate
>> Bioresource Engineering, McGill University
>> -- 
>> View this message in context:
>> http://www.nabble.com/Bin-Category-Labels-on-Axis-tp24042325p24042325.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Bin-Category-Labels-on-Axis-tp24042325p24063836.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list