0

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

Kai
  • 1
  • 1
    one option is that you can create a new column with boolean values (1 if year > 2017 else 0) and then in ggplot `aes()` or in `geom_point` use `group_by` argument and set it to the new column which will differentiate the colors. – memo May 05 '22 at 02:28

1 Answers1

0
  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

r2evans
  • 141,215
  • 6
  • 77
  • 149