0

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

s__
  • 9,270
  • 3
  • 27
  • 45
mauron
  • 9
  • 2
  • 1
    What quantity is shown on the radial axis? For example, `Sepal.Width` takes a value around 0.44. How is that value calculated? – Maurits Evers Jun 19 '20 at 03:34
  • It comes directly from the data, I am assuming that is not a calculation just to highlight the top three values. In my real world application the data comes verbatim from a survey. – mauron Jun 19 '20 at 13:26
  • Yes, I understand that. But in order to highlight certain top categories, we need to understand how these values are being calculated. `iris` contains 150 measurements of 4 variables (categories). Somehow `ggRadar` shows a single value for every category, and you like to highlight the top three categories. How are these single values calculated? – Maurits Evers Jun 20 '20 at 09:09
  • Hi Maurits in this case should be the highest value of the species for each variable – mauron Jun 21 '20 at 01:54

2 Answers2

1

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.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

My suggestion would be to identify the highest values you wish to highlight, and put them in a dataframe. Then use geom_richtext() to highlight.

YBS
  • 19,324
  • 2
  • 9
  • 27