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:
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:
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.