Search code examples
rggplot2radar-chart

ggRadar highlight top values in radar


Hi everyone I am making a a radar plot and I want to highlight the two highest values in the factors or levels. Highlight in this case is to make the text of the top tree values bold

require(ggplot2)
require(ggiraph)
require(plyr)
require(reshape2)
require(moonBook)
require(sjmisc)
ggRadar(iris,aes(x=c(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width)))

an example can be like this

Image

thank you


Solution

  • Here is a step-by-step example of how to highlight specific categories in a radar plot. I don't really see the point of all these extra dependencies (ggRadar etc.), as it's pretty straightforward to draw a radar plot in ggplot2 directly using polar coordinates.

    1. First, let's generate some sample data. According to OPs comments and his example based on the iris dataset, we select the maximal value for every variable (from Sepal.Length, Sepal.Width, Petal.Length, Petal.Width); we then store the result in a long tibble for plotting.

      library(purrr)
      library(dplyr)
      library(tidyr)
      df <- iris %>% select(-Species) %>% map_df(max) %>% pivot_longer(everything())
      df
      #    # A tibble: 4 x 2
      #  name         value
      #  <chr>        <dbl>
      #1 Sepal.Length   7.9
      #2 Sepal.Width    4.4
      #3 Petal.Length   6.9
      #4 Petal.Width    2.5
      
    2. Next, we make use of a custom coord_radar function (thanks to this post), that is centred around coord_polar and ensures that polygon lines in a polar plot are straight lines rather than curved arcs.

      coord_radar <- function (theta = "x", start = - pi / 2, direction = 1) {
          theta <- match.arg(theta, c("x", "y"))
          r <- if (theta == "x") "y" else "x"
          ggproto(
              "CordRadar", CoordPolar, theta = theta, r = r, start = start,
              direction = sign(direction),
              is_linear = function(coord) TRUE)
      }
      
    3. We now create a new column df$face that is "bold" for the top 3 variables (ranked by decreasing value) and "plain" otherwise. We also need to make sure that factor levels of our categories are sorted by row number (otherwise name and face won't necessarily match later).

      df <- df %>%
          mutate(
              rnk = rank(-value),
              face = if_else(rnk < 4, "bold", "plain"),
              name = factor(name, levels = unique(name)))
      
    4. We can now draw the plot

      library(ggplot2)
      ggplot(df, aes(name, value, group = 1)) +
          geom_polygon(fill = "red", colour = "red", alpha = 0.4) +
          geom_point(colour = "red") +
          coord_radar() +
          ylim(0, 10) +
          theme(axis.text.x = element_text(face = df$face))
      

      enter image description here

      Note that this gives a warning, which I choose to ignore here, as we explicitly make use of the vectorised element_text option.

      Warning message: Vectorized input to element_text() is not officially supported. Results may be unexpected or may change in future versions of ggplot2.