[R-sig-Geo] Dont show zero values in line graph

Rainer M Krug r.m.krug at gmail.com
Fri Jan 7 12:07:23 CET 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/07/2011 11:56 AM, Edzer Pebesma wrote:
> 
> 
> On 01/07/2011 09:43 AM, Rainer M Krug wrote:
>> On 01/07/2011 08:43 AM, Edzer Pebesma wrote:
>>
>>
>>> On 01/07/2011 04:11 AM, jroll wrote:
>>>>
>>>> Hey everyone,
>>>>    Im getting better at plotting my data but cant for the life of me figure
>>>> out how to show a line graph with missing data that doesnt continue the line
>>>> down to zero then back up to the remaining values.
>>>>
>>>> Consider the following
>>>> x<-c(1:5,0,0,8:10)
>>>> y<-1:10
>>>>
>>>> plot(0,0,xlim=c(0,10), ylim=c(0,10),type="n",main="Dont show the bloody 0
>>>> values!!")
>>>> lines(x~y, col="blue", lwd=2,)
>>>>
>>>> My data is missing the 6th and 7th values and they come in as NA's so i
>>>> change them to 0s but then the plot has these ugly lines that dive toward
>>>> the x axis then back up.  I would do bar plots but i need to show multiple
>>>> sets of data on the same and side by side bars doesnt do it for me.  
>>>>
>>>> So i need a line graph that starts and stops where 0s or missing values
>>>> exist.  Thoughts?
>>
>>> x<-c(1:5,NA,NA,8:10)
>>> y<-1:10
>>
>>> plot(0,0,xlim=c(0,10), ylim=c(0,10),type="n",main="Dont show the bloody 0
>>> values!!")
>>> lines(x~y, col="blue", lwd=2,)
>>
>> And just to complete it: to replace all the 0 in x with NA, you can do
>>
>> x[x==0] <- NA
>>
>> There is also the is.na() function which should do this, i.e.
>>
>> is.na(x) <- 0
>>
>> should (in my understanding) set all 0 values in x to NA - but it dose
>> not work, whereas setting e.g. all the 4s to NA works:
>>
>>> x<-c(1:5,0,0,8:10)
>>> is.na(x) <- 0
>>> x
>>  [1]  1  2  3  4  5  0  0  8  9 10
>>> is.na(x) <- 4
>>> x
>>  [1]  1  2  3 NA  5  0  0  8  9 10
>>
>>
>> Why is this? Is this a bug in R or in y understanding?
> 
> try ?is.na:

I did ...

> 
> Usage:
> 
>      NA
>      is.na(x)
>      ## S3 method for class 'data.frame'
>      is.na(x)
> 
>      is.na(x) <- value
> 
> Arguments:
> 
>        x: an R object to be tested: the default method handles atomic
>           vectors, lists and pairlists.
> 
>    value: a suitable index vector for use with ‘x’.

but my understanding of is.na() modified the help.
Thanks for pointing this out - now I know how I can use
is.na(...) <- ...

> 
> so,
> 
> is.na(x) <- 0
> 
> does nothing, as 0 is not a suitable index vector:

Absolutely true.

> 
>> x = 1:10
>> x[0]
> integer(0)
> 
> x[5] = 0
> is.na(x) <- x == 0
> x
>  [1]  1  2  3  4 NA  6  7  8  9 10
> 
> does what you wanted, I believe.

Yes - thanks a lot.

> 
>>
>> Cheers,
>>
>> Rainer
>>
>>>>
>>>> JR 
>>
>>
>>
> 


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Natural Sciences Building
Office Suite 2039
Stellenbosch University
Main Campus, Merriman Avenue
Stellenbosch
South Africa

Tel:        +33 - (0)9 53 10 27 44
Cell:       +27 - (0)8 39 47 90 42
Fax (SA):   +27 - (0)8 65 16 27 82
Fax (D) :   +49 - (0)3 21 21 25 22 44
Fax (FR):   +33 - (0)9 58 10 27 44
email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk0m8+sACgkQoYgNqgF2egoHmQCghTrTf0nYgEB4O8oHbnbm0Eit
gOQAn0//sj7xshLPFZ+QDxzXiyofv39J
=mSj4
-----END PGP SIGNATURE-----



More information about the R-sig-Geo mailing list