Rhea
Rhea

Reputation: 31

Is there a way to apply gghighlight on only some geoms in a ggplot?

I'm wondering if there's any way to have gghighlight only apply to geom_point() and geom_errorbar() but not geom_line(). If you look at the plot, gghighlight adds a new line to the graph connecting the two highlighted points, is there a way to stop this?

I've tried adding geom_line() underneath gghighlight but then it only plots the line between the highlighted points and the original line no longer shows up.

ggplot(data = faithBest, aes(x = x, y=avgRegEr.k))+
  geom_point(colour = "violet")+
  geom_line()+
  geom_errorbar(aes(ymin = avgRegEr.k-se, ymax = avgRegEr.k +se), colour = "violet")+
  labs(
    title = "Model Mean Squared Error VS Model Complexity",
    subtitle = "How does the MSE of the linear regression model change when adding predictors?",
    x = "Model Complexity (Number of Predictors)",
    y = "Model Mean Squared Error"
  )+
  scale_x_continuous(breaks = x)+
  gghighlight(x==4 | x==2, unhighlighted_params = list(colour = "lightsteelblue3"))

Here's the graph produced

Upvotes: 2

Views: 294

Answers (1)

Rhea
Rhea

Reputation: 31

I'm not sure if there is another way to do it, by I managed to get rid of the line by replacing geom_line() with geom_line(colour = '00FFFFFF')

00FFFFFF is the colour code for clear.

This gghighlight argument (which is the same as before):

unhighlighted_params = list(colour = "lightsteelblue3" 

is the factor needed for the line to show up, without it neither of the lines would appear.

This is the new graph

Upvotes: 0

Related Questions