0

image of data frame

How can I turn this data frame into a radar chart? I'm struggling to understand the use of max and min. I want Adams and Onuachu as the 2 variables and both on the same plot.

I tried radarchart(dat) which said there were non-numerical values. So I removed the 'Player' row and tried radarchart(df) and then the weird chart appeared in the photo.

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
  • 2
    Welcome to SO, footballstatistician! (1) Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). (2) Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269 and [mcve]. – r2evans Jan 31 '23 at 19:35
  • I'd suggest first reshaping your data, e.g. `df1 %>% tidyr::pivot_longer(-Player) %>% ggplot(aes(name, value, color = Player)) + geom_line() + geom_point() + coord_polar()` – Jon Spring Jan 31 '23 at 20:24

1 Answers1

0

The data structure required by radarchart from the fmsb package is a bit special. First, you could keep the player names but have to move them to the row names. Second, the first two rows should contain the min and max values, where the max value should be in the first row and the min value in second row. From the docs:

If maxmin is TRUE, this must include maximum values as row 1 and minimum values as row 2 for each variables, and actual data should be given as row 3 and lower rows. The number of columns (variables) must be more than 2.

Using some fake example data to mimic your real data you could achieve your desired result like so:

set.seed(123)

dat <- data.frame(
  Player = c("A", "B", "Max", "Min"),
  Feature1 = c(runif(2, 0, 1), c(1, 0)),
  Feature2 = c(runif(2, 0, 2), c(2, 0)),
  Feature3 = c(runif(2, 0, 3),c(3, 0)),
  Feature4 = c(runif(2, 0, 4),c(4, 0)),
  Feature5 = c(runif(2, 0, 5), c(5, 0))
)

# Move max and min values to first two rows
dat <- dat[c(3:4, 1:2), ]
# Move Player names to rownames
row.names(dat) <- dat$Player
dat <- dat[-1]

library(fmsb)

cols <- c("black", "red")

radarchart(dat, plty = 1, plwd = 3, pcol = cols)

legend("topright",
       legend = row.names(dat)[-c(1:2)],
       bty = "n", pch = 20, col = cols,
       text.col = "grey25", pt.cex = 2)

stefan
  • 90,330
  • 6
  • 25
  • 51