Search code examples
rggplot2gghighlight

How to highlight a line connecting a group in ggplot R?


I'm very new to R and I'm looking to highlight certain groups in my plot using gghighlight. For example, I want to highlight in red only groups 16 and 32 while leaving the rest of the lines in gray. My current code and output are below. I can't seem to highlight specific groups, I can only add color to all lines. I've tried gghighlight(grp==16) to try and highlight 16 but it says "Tried to calculate with group_by(), but the calculation failed".

example data

library(readxl)
library(ggplot2)
library(gghighlight)

guidelines <- read_excel("data.xlsx", sheet=1)
guidelines$step <- factor(guidelines$step, levels=c("First", "Highest", "Final"))

map <- ggplot(guidelines,
              aes(x = step, y = type, group = grp, color = factor(grp))) +
scale_color_hue(l=45) +
geom_line(linetype = 1) +
geom_line(position=position_jitter(w=0, h=0.05)) 


map + scale_y_continuous(breaks = c(1,2,3,4))
map + scale_y_continuous(breaks = c(1,2,3,4),
                         labels = c("Method 1", "Method 2", "Method 3", "Method 4"))

enter image description here


Solution

  • I tried to follow your way and did the following. I am not familiar with gghighlight(). I had a quick glance and thought you would need to use logical checks to highlight certain groups. If you can set up logical checks, I think you can use the function. In your question, you simply mentioned that you want to highlight specific groups. One way you can take would be to assign colors with scale_color_identity(). I created a sample data below. I basically assigned red to three groups (i.e., 1, 3, 5) in mutate(). The other groups got gray50 in the column, color.

    library(tidyverse)
    
    guidelines <- tibble(step = rep(c("First", "Highest", "Final"), times = 5),
                         type = c(2, 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 1, 3, 3),
                         grp = rep(1:5, each = 3))
    
    mutate(guidelines,
           step = factor(step, levels = c("First", "Highest", "Final")),
           color = if_else(grp %in% c(1, 3, 5), "red", "gray50")) -> guidelines
    
    
    ggplot(data = guidelines, aes(x = step, y = type, color = color, group = grp)) +
    geom_line(linetype = 1, position = position_jitter(w = 0, h = 0.05)) +
    scale_color_identity(guide = "legend", labels = c("Other groups", "Group 1, 3, 5")) +
    scale_y_continuous(breaks = c(1,2,3,4),
                       labels = c("Method 1", "Method 2", "Method 3", "Method 4"))
    

    enter image description here

    Potential solution with gghighlight

    I do not have your data. So this idea may/may not work. But I came up with the following idea. I modified guidelines once more. I created a new continuous variable (i.e., dummy), which I use for x-axis. This is a bit of twist since we are supposed to have a categorical variable on the axis. But this is necessary if you want to use gghighlight. As far as I understood gghighlight, we need logical check(s) in the function. Here, I wanted to highlight lines which max value is 3. This highlights group 5. Since you want to have two categorical variables, we gotta modify names on the x and y axis. If you can set up logical checks for the two groups in your data (16 and 32), I believe you can use this idea.

    library(gghighlight)
    
    mutate(guidelines,
           dummy = recode(.x = step, First = 1, Highest = 2, Final = 3),
           step = factor(step, levels = c("First", "Highest", "Final")),
           color = if_else(grp %in% c(1, 3, 5), "red", "gray50")) -> guidelines
    
    ggplot(data = guidelines, aes(x = dummy, y = type, color = color, group = grp)) +
    geom_line(size = 2, linetype = 1) +
    gghighlight(max(as.numeric(type)) == 3, label_key = grp) +
    labs(x = "Step", y = "Type") +
    scale_x_continuous(breaks = c(1,2,3),
                       labels = c("First", "Highest", "Final")) +
    scale_y_continuous(breaks = c(1,2,3,4),
                       labels = c("Method 1", "Method 2", "Method 3", "Method 4"))
    

    enter image description here