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".
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"))
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 withscale_color_identity()
. I created a sample data below. I basically assignedred
to three groups (i.e., 1, 3, 5) inmutate()
. The other groups gotgray50
in the column,color
.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 usegghighlight
. As far as I understoodgghighlight
, 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.