[R] Andrews plot
hadley wickham
h.wickham at gmail.com
Mon Sep 7 15:50:03 CEST 2009
On Mon, Sep 7, 2009 at 8:36 AM, Petr PIKAL<petr.pikal at precheza.cz> wrote:
> Dear all
>
> Colleague of mine ask me if R is capable of Andrews plot like
> andrewsplot(x) in Matlab.
>
> Quick search did not reveal anything but before I start to write any
> routine I would like to ask this ingenious audience if there is any
> implementation of Andrews plots somewhere.
Here's the code I use in the tourr package:
#' Compute Andrews' curves
#'
#' This function takes a numeric vector of input, and returns a function which
#' allows you to compute the value of the Andrew's curve at every point along
#' its path from -pi to pi.
#'
#' @param x input a new parameter
#' @return a function with single argument, theta
#'
#' @examples
#' a <- andrews(1:2)
#' a(0)
#' a(-pi)
#' grid <- seq(-pi, pi, length = 50)
#' a(grid)
#'
#' plot(grid, andrews(1:2)(grid), type = "l")
#' plot(grid, andrews(runif(5))(grid), type = "l")
andrews <- function(x) {
n <- length(x)
y <- rep(x[1] / sqrt(2), length(t))
function(t) {
for(i in seq(2, n, by = 1)) {
val <- i %/% 2 * t
y <- y + x[i] * (if(i %% 2 == 0) sin(val) else cos(val))
}
y / n
}
}
> I know about parallel coordinate plots in lattice (although I do not use
> them as I am not sure what the plot tells me :-).
If you don't understand parallel coordinates plots, I think you're
going to find Andrew's curves even harder to understand.
Hadley
--
http://had.co.nz/
More information about the R-help
mailing list