Home > OS >  Data frame --> Radar chart
Data frame --> Radar chart

Time:02-02

enter image description hereHow 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.

CodePudding user response:

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)

  • Related