SecretIndividual
SecretIndividual

Reputation: 2529

Visualize clock effect by coloring ggplot data bases on criteria

I am trying to visualize the correlation between the scheduled arrival time and the actual arrival time of data in the flights package.

Doing this I generated the plot as shown here: enter image description here Code:

library(nycflights13)
library(ggplot2)
attach(flights)

ggplot(flights, aes(x = sched_arr_time, y = arr_time)) + 
  geom_point(size = 2, color='darkblue', alpha = 0.2) +
  geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95, color='red')

I would like to color all data that is effected by the clock passing 00.00 orange.

How would I do this?

Upvotes: 2

Views: 62

Answers (1)

dc37
dc37

Reputation: 16178

I guess you made a mistake in your question because based on your conditions all points will be orange.

Anyway, if you would like to color conditionally your points, there is multiple ways of doing it, you can for example create an extra column with an ifelse statement containing your color of interest, then plot your points using the new column as color argument in your aes and call scale_color_identity function to apply the color pattern contained in this column:

library(dplyr)
library(ggplot2)
flights %>% mutate(Color = ifelse(sched_arr_time < 2400 & arr_time  > 0, "orange","darkblue")) %>%
  ggplot(aes(x = sched_arr_time, y = arr_time, color = Color))+
  geom_point(alpha = 0.2)+
  geom_smooth(color = "red")+
  scale_color_identity()

enter image description here

Upvotes: 1

Related Questions