How to manually change the color of certain geom_point?

555 Views Asked by At

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

1

There are 1 best solutions below

0
On
  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