Stefan Christoph
Stefan Christoph

Reputation: 43

ggplot: Is it possible to plot different colors points for different time windows?

I have a question regarding ggplot.

I have a dataset that consists of different timepoints "time", x and y coordinates (named X_MA and Y_MA), see an excerpt of my dataset:

      # A tibble: 6 x 9
     ..1 participant       time..3 time..4             time_2     X  X_MA     Y  Y_MA
   <dbl> <chr>               <dbl> <dttm>               <dbl> <dbl> <dbl> <dbl> <dbl>
1 261548 J5Q99IF1    1550093328320 2019-02-13 21:28:48      1   321  321    359  359 
2 261549 J5Q99IF1    1550093328353 2019-02-13 21:28:48      2   351  351    485  485 
3 261550 J5Q99IF1    1550093328375 2019-02-13 21:28:48      3   387  387    649  649 
4 261551 J5Q99IF1    1550093328401 2019-02-13 21:28:48      4   349  349    302  302 
5 261552 J5Q99IF1    1550093328419 2019-02-13 21:28:48      5   311  344.   482  455.
6 261553 J5Q99IF1    1550093328438 2019-02-13 21:28:48      6   188  317.   424  468.

I know how to plot the whole dataset into a heatmap by using the following code:

ggplot(Test_R_TAU_first2sec, aes(x=X_MA, y=Y_MA) ) + 
  stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) + 
  scale_fill_distiller(palette=8, direction=-1) + 
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0, 0)) + 
  theme(legend.position='n_one') + 
  geom_point(size=1)

Unfortunately all points in the graph have the same color (black):

example output.

However I want the points of the first 2 second to be in a different color than the next 10 seconds and again a different color for the last 2 seconds. Do you know what I have to use as a command in order to get these different colors for different time windows (time proxy variable is time_2)?

I now tried the following code:

ggplot(Test_R_TAU_2_732019,aes(x = X_MA, y = Y_MA, color =  cut(time_2, breaks = c(1,108,1001,1121), labels = c("First two seconds","Middle two seconds","Last period")))) + stat_density_2d(aes(fill =..level..), geom = "raster", contour = FALSE) + scale_fill_distiller(palette=8, direction=-1) + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) + theme(legend.position='n_one') + geom_point(size=1) + guides(color=guide_legend(title="Time category"))

Unfortunately I get this error:

Error: (converted from warning) Computation failed in `stat_density2d()`:
missing value where TRUE/FALSE needed

Can anyone help?

Upvotes: 2

Views: 254

Answers (1)

Harvey Ellis
Harvey Ellis

Reputation: 606

You can do something like this:

ggplot(data2,aes(x = X_MA, y = Y_MA, 
                 color =  cut(time, breaks = c(0,2,12,1000), labels = c("First two 
                 seconds","Middle two seconds","Last period")))) + 
       stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) + 
       scale_fill_distiller(palette=8, direction=-1) + 
       scale_x_continuous(expand = c(0, 0)) + 
       scale_y_continuous(expand = c(0, 0)) + 
       theme(legend.position='n_one') + 
       geom_point(size=1) +
       guides(color=guide_legend(title="Time category"))

Cut will split you data set up into intevals, which you can name here. Note that you will need to change the numbers for the intervals - I don't know what the first time reading is in your dataset, but I have left here for illustration.

The guides bit at the bottom is there simply to name the legend.

Upvotes: 1

Related Questions