Re: [R] "spreading out" a numeric vector

From: jim holtman <jholtman_at_gmail.com>
Date: Sat, 22 Mar 2008 20:58:39 -0500

Is this close to what you want?

> spread <- function(x, mindiff=0.5){

+ unique(as.integer(x / mindiff) * mindiff) + }
>
> x <- c(1,2,seq(2.1,2.3,0.1),3,4)
> x
[1] 1.0 2.0 2.1 2.2 2.3 3.0 4.0
> spread(x)

[1] 1 2 3 4
>
> spread(x,.2)

[1] 1.0 2.0 2.2 3.0 4.0
>

On Sat, Mar 22, 2008 at 8:03 PM, Levi Waldron <leviwaldron_at_gmail.com> wrote:
> I am creating a timeline plot, but running into a problem in positions
> where the values to plot are too close together to print the text
> without overlap. The simplest way I can think of to solve this
> (although there may be other ways?) is to create a new numeric vector
> whose values are as close as possible to the original vector, but
> spread out to a given minimum difference. For example, take the
> following vector:
>
> > x <- c(1,2,seq(2.1,2.3,0.1),3,4)
> > x
> [1] 1.0 2.0 2.1 2.2 2.3 3.0 4.0
> >
>
> However, suppose I don't want to plot any of these points with less
> than 0.5 between them. The problem could be solved by a function that
> behaved something like this:
>
> > x2 <- spread(x,mindiff=0.5)
> > x2
> [1] 0.5 1.0 1.5 2.0 2.5 3.0 4.0
>
> Or for a minimum difference of 0.2,
> > x2 <- spread(x,mindiff=0.2)
> > x2
> [1] 1.0 1.8 2.0 2.2 2.4 3.0 4.0
>
> Thus, if there is a cluster of close values, spreading the values may
> require changing values which previously weren't too close to their
> nearest neighbors.
>
> Then I could use segments() to draw lines from the timeline to where
> the shifted text is printed, labeling tics on the timeline accurately
> but keeping the text from overlapping.
>
> Any ideas on how to program this or how to do it using built-in
> functions would be greatly appreciated.
>
> ______________________________________________
> 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Received on Sun 23 Mar 2008 - 02:11:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 23 Mar 2008 - 04:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive