'How do you add a color criteria to a scatterplot in ggplot2?

I am creating a scatterplot to show the relationship between Very Active Minutes of exercise and Calories burned. In my scatterplot I want the points to be green after 30 minutes of activity. Below is the code I am currently using, but I do not know how to add a criteria to change color.

ggplot(data=daily_activity) +
  geom_point(mapping=aes(x=VeryActiveMinutes, y=Calories), color="darkred") +
  geom_smooth(mapping=aes(x=VeryActiveMinutes, y=Calories)) +
  labs(title="The Relationship Between Very Active Minutes and Calories Burned", 
       caption = "Data collected from the FitBit Fitness Tracker Data on Kaggle")


Solution 1:[1]

As suggested by @stefan color based on the ifelse statement, and then apply those colours to the graph using scale color identity:

Sample code:

daily_activity %>% 
    mutate(Color = ifelse(VeryActiveMinutes > 30, "green", "red"))%>% 
    ggplot(aes(x=VeryActiveMinutes, y=Calories, color=Color)) +
      geom_point() +
      geom_smooth(mapping=aes(x=VeryActiveMinutes, y=Calories)) +
      labs(title="The Relationship Between Very Active Minutes and Calories Burned", 
           caption = "Data collected from the FitBit Fitness Tracker Data on Kaggle")

Output:

enter image description here

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 Rfanatic