Martin Petri Bagger
Martin Petri Bagger

Reputation: 2317

Plotting a discrete variable over time (scarf plot)

I have time series data from a repeated-measures eyetracking experiment.

The dataset consists of a number of respondents and for each respondent, there is 48 trials. The data set has a variable ('saccade') which is the transitions between eye-fixations and a variable ('time') which ranges for 0-1 for each trial. The transitions are classified into three different categories ('ver', 'hor' and 'diag').

Here is a script that will create a small example data set in python (one participant and two trials):

import numpy as np
import pandas as pd

saccade1 = np.array(['diag','hor','ver','hor','diag','ver','hor','diag','diag',
'diag','hor','ver','ver','ver','ver','diag','ver','ver','hor','hor','hor','diag',
'diag','ver','ver','ver','ver'])

time1 = np.array(range(len(saccade1)))/float(len(saccade1)-1)

trial1 = [1]*len(time1)

saccade2 = np.array(['diag','ver','hor','diag','diag','diag','hor','ver','hor',
'diag','hor','ver','ver','ver','ver','diag','ver','ver','hor','diag',
'diag','hor','hor','diag','diag','ver','ver','ver','ver','hor','diag','diag'])
time2 = np.array(range(len(saccade2)))/float(len(saccade2)-1)

trial2 = [2]*len(time2)

saccade = np.append(saccade1,saccade2)
time = np.append(time1,time2)
trial = np.append(trial1,trial2)
subject = [1]*len(time)

df = pd.DataFrame(index=range(len(subject)))
df['subject'] = subject
df['saccade'] = saccade
df['trial'] = trial
df['time'] = time

Alternatively I have made a csv-file with the same data which can be downloaded here

I would like to be able to make a so-called scarf plot to visualize the sequence of transitions over time, but I have no clue how to make these plots.

I would like plots (for each participant separately) where time is on the x-axis and trial is on the y-axis. For each trial I would like the transitions represented as colored "stacked" bars.

The only example I have of these kinds of plots are in the book "Eye Tracking - A comprehensive guide to methods and measures" (fig. 6.8b) link

Can anyone tell/help me in doing this?

(I can deal which python or R programming - preferably python)

Upvotes: 1

Views: 1049

Answers (1)

Thierry
Thierry

Reputation: 18487

Here is a solution in R using ggplot2. You need to recode time2 so that it indicates the enlapsed time instead of the total time.

library(ggplot2)
dataset <- read.csv("~/Downloads/example_data_for_scarf.csv")
dataset$trial <- factor(dataset$trial)
dataset$saccade <- factor(dataset$saccade)
dataset$time2 <- c(0, diff(dataset$time))
dataset$time2[dataset$time == 0] <- 0
ggplot(dataset, aes(x = trial, y = time2, fill = saccade)) + 
    geom_bar(stat = "identity") + 
    coord_flip()

scarf plot

Upvotes: 2

Related Questions