radarchart: Change background color

121 Views Asked by At

I want to change the background color of the radar chart. I don't see any argument that allow me to do this. Help?

Here's what I have so far:

structure(list(Acceptance = c(16, 4, 13), `Committed Action` = c(16, 
4, 11), Diffusion = c(16, 4, 7), `Present Moment` = c(16, 4, 
10), `Self as Context` = c(16, 4, 11), Values = c(16, 4, 12)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

# DF Structure

Acceptance `Committed Action` Diffusion `Present Moment` `Self as Context` Values
       <dbl>              <dbl>     <dbl>            <dbl>             <dbl>  <dbl>
1         16                 16        16               16                16     16
2          4                  4         4                4                 4      4
3         13                 11         7               10                11     12

Graph:

radarchart(df, axistype=1,
           #custom polygon
           pcol="darkblue", pfcol=rgb(0.2,0.5,0.5,0.5), plwd=3,
           #custom the grid
           cglcol="grey50", cglty=1, axislabcol="black", caxislabels=seq(4,16,length.out = 5), 
           cglwd=2,
           #custom labels
           vlcex=1.8, calcex=1.8) 

This is what I get:

Radar Graph

I have tried:


g <- radarchart(df, axistype=1,
           #custom polygon
           pcol="darkblue", pfcol=rgb(0.2,0.5,0.5,0.5), plwd=3,
           #custom the grid
           cglcol="grey50", cglty=1, axislabcol="black", caxislabels=seq(4,16,length.out = 5), 
           cglwd=2,
           #custom labels
           vlcex=1.8, calcex=1.8) 

g + theme(panel.background = element_rect(fill = "lightgray"))

# Output: NULL

Is there no way to add background color?

1

There are 1 best solutions below

1
On BEST ANSWER

If your question addresses the use of radarchart from the fmbs package, the first thing to note is that this produces a plot using base R graphics. Modifying the background using theme() won't work because g is not a ggplot object.

Option 1: add a background to the entire plotting area setting graphical parameters for plotting

library(fmsb)

par(bg = "lightgray")

fmsb::radarchart(df, axistype=1,
                #custom polygon
                pcol="darkblue", pfcol=rgb(0.2,0.5,0.5,0.5), plwd=3,
                #custom the grid
                cglcol="grey50", cglty=1, axislabcol="black", caxislabels=seq(4,16,length.out = 5), 
                cglwd=2, 
                #custom labels
                vlcex=1.8, calcex=1.8) 

enter image description here

Option 2: If you want to use ggplot, you could install the ggradar package and produce your radar chart that way. It will look different but here's a start. https://github.com/ricardo-bion/ggradar

devtools::install_github("ricardo-bion/ggradar", 
                          dependencies = TRUE)
library(ggradar)
df<-df %>%
  as_tibble(rownames = "group")

# note that in the below I'm only selecting the third row of your dataframe
# this is to reproduce your plot above
ggradar(df[3,], grid.min = 4, grid.mid = 10, grid.max = 16,
        values.radar = c(4, 10, 16), 
        gridline.mid.colour = "grey",
        axis.label.size=4,
        axis.label.offset   = 1.1) + 
  theme(panel.background = element_rect(fill = "lightgray"))

enter image description here