[R] Barplot With Selected X-Axis Region

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Mar 24 14:36:40 CET 2009



Gundala Viswanath wrote:
> Dear all,
> 
> I have a data that looks like this:
> 
>> print(dat)
>              V1   V2
> 1     -43342073   14
> 2     -43337730    4
> 3     -43284676    1
> ....
> 11372  75188572   11
> 11373  75206165    6
> 11374  75262720   24
> 
> 
> What I want to do is to have a barplot where x-axis
> is taken from V1 and y-axis taken from V2.
> 
> But I only want to plot the those region where V1 >= -500
> and <= 500.
> 
> But some how this snippet doesn't seem to work:
> 
>> xlim = c(-500,500)
>> barplot(as.matrix(dat[dat$V1 >= -500 && dat$V1 <=500,dat$V2]),xlim=xlim,axes=TRUE, main =nm);

You need to change

dat$V1 >= -500 && dat$V1 <=500

to

dat$V1 >= -500 & dat$V1 <=500

and read about the difference between & and &&.


So probably you want something like:

   subsetdat <- dat[dat$V1 >= -500 & dat$V1 <=500,]
   barplot(subsetdat$V2, names.arg=subsetdat$V1)

Uwe Ligges





> 
> What's wrong with my approach.
> 
> So yes, I do want the V1 information, hence I am aware that
> I dont' want:
> 
>> plot(hist(dat[dat >=-500 & dat <=500]))
> 
> - Gundala Viswanath
> Jakarta - Indonesia
> 
> ______________________________________________
> 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