Home > OS >  Why do RadarCharts graph incorrectly in R? (fmsb)
Why do RadarCharts graph incorrectly in R? (fmsb)

Time:10-09

create_beautiful_radarchart <- function(data, color = "#800000", 
                                        vlabels = colnames(data), vlcex = 0.7,
                                        caxislabels = NULL, title = NULL, ...){
  radarchart(
    data, axistype = 1,
    pcol = color, pfcol = scales::alpha(color, 0.5), plwd = 2, plty = 1,
    cglcol = "grey", cglty = 1, cglwd = 0.8,
    axislabcol = "grey",
    vlcex = vlcex, vlabels = vlabels,
    caxislabels = caxislabels, title = title, ...
  )
}

colors <- c("#00AFBB", "#E0115F", "#800000", "orange")
titles <- c("UnderWeight", "NormalWeight", "OverWeight", "Obese")

for(i in 1:4){
  create_beautiful_radarchart(
    data = df[c(1,2,3,4),], caxislabels = c(5000,10000,15000,20000),
    color = colors[i], title = titles[i],
    seg=3)
}

Why does my code produce radarcharts like:

Radarchar

Here is my entire dataset:

NORTHEAST NORTHWEST SOUTHEAST SOUTHWEST
UNDERWEIGHT 8914.424 9223.351 0.00 7778.771
NORMALWEIGHT 11151.782 10032.507 13286.81 7246.518
OVERWEIGHT 10818.594 11503.929 10846.20 10786.970
OBESE 16606.763 14198.111 16139.57 14752.032

CodePudding user response:

The first two rows of your data frame have to be the maximum and minimum values you want plotted for that series. Since you are filling the areas, you will need to make the colors for the macimum and minimum values NA. You can automate the ranges for each series like this:

range <- as.data.frame(lapply(df, function(x) rev(range(pretty(x)))))

And presumably you want 4 separate radar plots, in which case you can do:

for(i in 1:4){
  create_beautiful_radarchart(
    data = rbind(range, df[i,]), caxislabels = c(5000,10000,15000,20000),
    color = c(NA, NA, colors[i]), title = titles[i], 
    seg=3)
}

enter image description here

  • Related