Home > database >  ggRadar showing different values than dataframe
ggRadar showing different values than dataframe

Time:11-10

I am fairly new to R and I am working on a radar plot with multiple y-values and facets. I want to create three seperate radar plots out of my dataframe instead of just one, depending on the location of my points. When I filter my df according to the points I want, it is not displayed with the correct values.

p.tot <- ggRadar(df.Plot.PI.total, mapping = aes(colour = scenario, facet = PlotNames),
            size=3, alpha = 0, ylim = c(1,1)) 
    theme_bw() 
    scale_color_manual(name="Scenario", values = cbPalette.PI) 
    scale_fill_manual(values = cbPalette.PI, name="Scenario") 
    labs(title = "Changes in Protection Indexes") 
    theme(plot.title = element_text(size = 20,face = "bold"),
          strip.text.x = element_text(size = 14),
          legend.title = element_text(face="bold",size = 18), legend.text = element_text(size = 16),
          legend.position = "bottom",
          text = element_text(size=18),
          axis.text = element_text(size=14),axis.title = element_text(size=14))

enter image description here

Plotting the whole df worked fine. For my work I want to plot three different plots, one for each group of points. So I applied a filter to the df to only plot the points of interest.

df.Plot.PI.SUB <- df.Plot.PI.total %>% filter(str_detect(PlotNames, "SUB_"))

When I plot the shortened data however, some points in the radarplots are not displayed correctly, even though the data is still the same!

enter image description here

Can somebody tell how to solve this problem? Thanks a lot!

CodePudding user response:

The issue is that ggRadar by default rescales all continuous variables in the dataset (see ?ggRadar):

rescale A logical value. If TRUE, all continuous variables in the data.frame are rescaled.

As the rescaling depends on the data provided you will get a different result when subsetting your data. To fix that rescale your data manually and set rescale=FALSE.

As you provided no example data I created a minimal reprex based on mtcars.

First I reproduce your issue with the default rescaling. Here we get different plots when dropping the cyl=8 values.

library(ggiraphExtra)
library(dplyr)
library(scales)
library(tibble)
library(ggplot2)
library(patchwork)

mtcars_radar <- mtcars %>% 
  as_tibble(rownames = "group") %>% 
  mutate(cyl = factor(cyl),
         across(!c(group, cyl), rescale)) %>% 
  tail(4) %>% 
  select(1:10)

p1 <- ggRadar(mtcars_radar, mapping = aes(facet = cyl))
p2 <- ggRadar(filter(mtcars_radar, cyl != 8), mapping = aes(facet = cyl))


p1 / p2

But when setting rescale=FALSE we get the same radar charts for cyl=4 and cyl=6:


p3 <- ggRadar(mtcars_radar, mapping = aes(facet = cyl), rescale = FALSE)

p4 <- ggRadar(filter(mtcars_radar, cyl != 8), mapping = aes(facet = cyl), rescale = FALSE)

p3 / p4

  • Related