[R] Plotting only masked part of data

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Tue Jan 25 15:20:06 CET 2005


On Tue, 25 Jan 2005 14:57:18 +0100 Joakim Hove wrote:

> 
> Hello,
> 
> I have x and y data to plot (synthetic example):
> 
>   x <- seq(0,4*pi,by=0.1)
>   y <- sin(x)
> 
> I then want to plot (x,y) in those points where abs(y) is smaller than
> 0.5. As a first approximation
> 
>   plot(x[abs(y) < 0.5],y[abs(y) < 0.5])
> 
> is quite close - however I want to plot with lines, i.e. type="l", and
> then I get solid lines connecting the endpoint of one "active" region
> to the start of the next active region, I would prefer to get rid of
> those.
> 
> Lineart:
> -------
>  
>    _____               ____
>   / (1) \             / (3)\
>  /       \           /      \
> /         \         /        \
>            \       /          \
>             \_____/
>               (2)
> 
> 
> The problem is the horisontal lines shown as (1), (2) and (3) in the
> schematic figure above, I would like to get rid of those,
> i.e. retaining only four disconnected segments. I have started on a
> programming based solution, plotting one piece at a time, but it is
> *very* ugly!
> 
> Is there a simple general solution to my problem?

That depends what general means. For this particular problem, the
following works:
  y2 <- y
  is.na(y2) <- abs(y) >= 0.5
  plot(x, y2, type = "l")

hth,
Z

> 
> 
> Regards
> 
> Joakim Hove
> 
> -- 
> Joakim Hove
> hove AT ift uib no
> Tlf: +47 (55 5)8 27 90 
> Fax: +47 (55 5)8 94 40
> http://www.ift.uib.no/~hove/
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list