1

I have a tibble containing 4 columns: a participant index, a timestamp, a x-coordinate and a corresponding y-coordinate in a tidy format. They represent mouse movement trajectories that I want to plot. See the following head-Output (please note that R does not show any decimal points but there are )

> head(coordinates_table)
# A tibble: 6 x 4
     VP run_timestamp UserCircleXPos UserCircleYPos
  <int>         <int>          <dbl>          <dbl>
1     1             0           499.           502.
2     1             1           499.           502.
3     1             2           499.           502.
4     1             3           499.           502.
5     1             4           499.           502.
6     1             5           499.           502.

The dput to recreate at least the head is

structure(list(VP = c(1L, 1L, 1L, 1L, 1L, 1L), run_timestamp = 0:5, 
UserCircleXPos = c(499.120831511463, 499.120831511463, 499.120831511463, 
499.120831511463, 499.120831511463, 499.120831511463), UserCircleYPos = c(501.758336977075, 
501.758336977075, 501.758336977075, 501.758336977075, 502.344375167638, 
502.344375167638)), row.names = c(NA, 6L), class = "data.frame")

Using geom_path from the ggplot2-package it was no problem to plot the trajectories for each participant (using the color aes to separate them). Trajectory plot per participant:

Trajectory plot per participant

However, I would also like to plot the average trajectory with some smoothing. My first approach was to calculate the average for every timestamp by using timestamp as a grouping variable.

library(magritr) # for pipe operator
library(dplyr) # for group_by(), summarise()
library(ggplot2) # for ggplot()
coordinates_table %>%
group_by(run_timestamp) %>%
summarise(mean_x=mean(UserCircleXPos), mean_y=mean(UserCircleYPos)) %>%
ggplot(., aes(x=mean_x, y=mean_y)) + 
  geom_path() +
  scale_y_continuous(limits = c(0, +1000)) +
  scale_x_continuous(limits = c(0, +1000)) +
  geom_smooth(span = 0.1,se = FALSE)

Which produces this plot:

this plot

Please note that in this example I also tried to average the data using the geom_smooth function. However, this produces the same problem as in this thread as geom_smooth does not work in order of the rows of the input. I cannot use the solution from the other thread though as my x-coordinates are not unique and therefore cannot be used as a grouping variable.

Also as the x-y-coordinates per timestamp are not the same for each participant (sometimes not even close) a regular average might not be suitable.

Therefore I'm looking for a solution to plot a clean average trajectory plot with some smoothing on the data. I haven't used a LOESS approach ever before but I think this might be what I want. I just need a push into the right direction.

camille
  • 16,432
  • 18
  • 38
  • 60
Dom42
  • 147
  • 11
  • what about using `rowid_to_column` to create a unique ID for each x-y pair and using that as a grouping variable? – Ben G Aug 09 '18 at 14:06
  • @BenG Thanks for the idea. I might have misphrased it in the initial post because I misunderstood it myself: it's not the fact that the x-values cannot be used as a grouping variable but rather the fact that I have several participants whose data would need to be averaged. Right now I'm also unsure whether the averaging per timestamp is the correct approach. – Dom42 Aug 10 '18 at 08:07
  • So, I don't think I can make a useful answer with the data you provided, but I think what you need to do is do the "smoothing" (the averaging) in the data set itself and then plot it with `geom_path`. – Ben G Aug 10 '18 at 10:49
  • @BenG I think the same way about the averaging: I just haven't figured out what would be the best averaging approach. Nonetheless thank you for your idea! – Dom42 Aug 10 '18 at 13:49

0 Answers0