I am trying to learn about Andrews plots by doing. I know R has the `andrews` package that uses the base plotting system, but I wanted to use `ggplot2`. I followed the [`andrews_curves`](https://github.com/holoviz/hvplot/blob/master/hvplot/plotting/andrews_curves.py) function implementation in the `pandas` library.

I have managed to translate the data transformation steps of the Python function:

```
andrews <- function(df, class_column, samples=200) {
  t <- seq(-pi, pi, length.out = samples) 

  vals <- t(
    data.matrix(                    
      df[, -which(names(df) %in% class_column)]
    )
  )

  curves <- outer(vals[1, ], rep(1, length(t))) 

  for (i in 2:nrow(vals)) {                       
    ft = (i %/% 2) * t                          
    if (i %% 2 == 0) {                                
      curves <- curves + outer(vals[i, ], sin(ft))
    } else {
      curves <- curves + outer(vals[i, ], cos(ft))
    }  
  }

  df_out <- data.frame( 
    t = rep(seq_len(samples), nrow(curves)),
    sample =  rep(seq_len(nrow(curves)), ncol(curves)),
    values = as.vector(t(curves)),
    class_column = rep(df[, class_column], samples)
  ) 

  df_out 
} 

```

Unfortunately, I am a beginner in Python and I do not understand the plotting systems (60-69 in [`andrews_curves`](https://github.com/holoviz/hvplot/blob/master/hvplot/plotting/andrews_curves.py)). I just want to replicate [this plot](https://pandas.pydata.org/docs/reference/api/pandas.plotting.andrews_curves.html) by using the same data, but I get something that is really off:

```r
iris <- read.csv('https://raw.github.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/iris.csv')

adrews_data <- andrews(iris, "Name", 30)

library(ggplot2)
ggplot(adrews_data, aes(x = t, y = values, color = class_column, group = interaction(class_column, sample))) +   
  geom_line(size = 1.2)

```

![](https://i.imgur.com/wStH2kt.png)

Created on 2022-01-26 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)