Search code examples
rggplot2ggradar

Change axis title text color in ggradar


I want the tiles around the radar chart (eg mpg, cyl, disp, etc) to be a different color. Is there a way to do this in ggradar? I have tried to do this with the theme() argument but doesn't seem to be working. There is an argument within ggradar to change font but not color.

mtcars %>%
  add_rownames( var = "group" ) %>%
  mutate_each(funs(rescale), -group) %>%
  tail(4) %>% select(1:10) %>% 
  ggradar() + 
  theme(
    axis.title = element_text(color = 'red')
  )


Solution

  • After a look at the docs and the source code of ggradar() I'm afraid there is no option to set the "axis" text color. Actually the axis text is added via multiple geom_text layers which is the reason why theme options will not work.

    One option would be to manipulate the ggplot object by first identifying the "text" layers (There are a bunch of (text) layers) and afterwards setting the text colors manually:

    library(ggradar)
    library(ggplot2)
    library(dplyr)
    
    p <- mtcars %>%
      tibble::rownames_to_column(var = "group") %>%
      mutate(across(-group, scales::rescale)) %>%
      tail(4) %>% 
      select(1:10) %>% 
      ggradar()
    
    # Text layers
    is_text <- sapply(p$layers, function(x) inherits(x$geom, "GeomText") && any(x$data$text %in% names(mtcars)))
    
    p$layers[is_text] <- lapply(p$layers[is_text], function(x) { x$aes_params$colour <- "red"; x})
    
    p