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 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). I just want to replicate this plot by using the same data, but I get something that is really off:
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)

Created on 2022-01-26 by the reprex package (v2.0.1)

