'How to manually change the color of certain geom_point?

I'm fairly new to R and am wondering how can I manually change certain geom_point's color?

For instance, here's the average velocity of Justin Verlander from 2008 - 2022

JVvelo_year%>%
ggplot(aes(factor(year), velo, group = 1))+
geom_line()+
geom_point(size = 5, color = "salmon")+
xlab('Season')+
ylab('Average Velocity')

enter image description here

I'd like to change the dots before 2017 to darkblue to align with the Tigers' uniform and keep the dots after 2017 orange.

Thank you



Solution 1:[1]

  1. Indicate which row by adding a column:

    mtcars$interesting <- "no"
    mtcars$interesting[5] <- "yes"
    ggplot(mtcars, aes(mpg, disp)) + geom_point(aes(color = interesting), size = 5)
    

    one call to geom_point

    This has the advantage of being more ggplot2-canonical, and will fluidly support legends and other aesthetic-controlling devices.

  2. Plot two sets of points, setting data= each time.

    ggplot(mtcars, aes(mpg, disp)) +
      geom_point(size = 5, color = "blue", data = ~subset(., interesting == "no")) + 
      geom_point(size = 5, color = "red", data = ~subset(., interesting == "yes"))
    

    individual geom point calls

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r2evans