Home > database >  Change axis title text color in ggradar
Change axis title text color in ggradar

Time:05-12

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')
  )

CodePudding user response:

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

CodePudding user response:

The circumferential axis labels in ggradar are not "really" axis labels. They are drawn as geom_text layers, which is why they don't change when different theme elements are applied..

I don't see an option to change them directly, but it's possible to supply the colours to the appropriate layers' aesthetics like this:

p <- mtcars %>%
  rownames_to_column( var = "group" ) %>%
  mutate(across(where(is.numeric), scales::rescale)) %>%
  tail(4) %>% 
  select(1:10) %>% 
  ggradar() 

p$layers[[1]]$aes_params <- c(p$layers[[1]]$aes_params, colour = "red")
p$layers[[5]]$aes_params <- c(p$layers[[5]]$aes_params, colour = "red")
p$layers[[6]]$aes_params <- c(p$layers[[6]]$aes_params, colour = "red")

p

enter image description here

  • Related