**Double tap sul link e clicca su “open in a new tab”

Clicca qui per scaricare l’intera cartella con dati e script

Clicca qui per scaricare la cartella SingleTrials_01

Clicca qui per scaricare la cartella SingleTrials_02

First Glance

Touchscreen Analysis

library(ggplot2)
library(tidyverse)
library(gridExtra)

#upload files
files <- list.files(path = "./DataCleaned/", full.names = TRUE, pattern = paste(".*", ".txt", sep = "")) 
dataT <- do.call(rbind, lapply(files, function(x) read.table(x,header = TRUE)))
raw <-dim(dataT)[1]

# Distance contact points
euclideanDist <- function(x1, y1, x2, y2){sqrt((x2 - x1)^2 + (y2 - y1)^2)}
dataT$fga <-sqrt((dataT$ThumbXinMM-dataT$IndexXinMM)^2 + (dataT$ThumbYinMM -dataT$IndexYinMM)^2) 

#Data Cleaning
dat1 <- dataT %>% 
        filter(ThumbX != -1,  RTs <= 1.2,  RTs > .2, fga < Distance +40, fga > Distance -40)
        filtered<- dim(dat1)[1]

#Removed trial
1 - (filtered/raw)
## [1] 0.07708333
#Variables of interest
W = dat1%>% group_by(Distance, ExpCondition) %>%
            summarise(  
            fga = sd(fga))

#Fga ~ Distance
ggplot(W, aes(Distance, fga, colour = ExpCondition )) +
          geom_point() +
          theme_minimal() +
          
          geom_smooth(method = "lm", se = FALSE)+
          theme_minimal()

W2 = dat1%>% group_by(Distance, ExpCondition,TargetIndexYInMM ) %>%
             summarise(  
             fga = sd(fga))

W2$TargetIndexYInMM <- round(W2$TargetIndexYInMM)



#check1: What is the influence of Y-Manipulation on fga?

ggplot(W2, aes(Distance, fga, group = ExpCondition)) +
           geom_line() +
           geom_point() +
           facet_wrap(~TargetIndexYInMM, ncol = 5) +
           theme_minimal()

##Kinematics

# Import data -------------------------------------------------------------

files <- list.files(path = "./DataCleaned/", full.names = TRUE, pattern = paste(".*", ".csv", sep = "")) # get only files with .csv extension
dataAll <- do.call(rbind, lapply(files, function(x) read_csv(x)))
dataAll1 <-dataAll
dataAll1$PointXpos <- rowMeans(cbind(dataAll1$ThumbXpos, dataAll1$IndexXpos))
dataAll1$PointYpos <- rowMeans(cbind(dataAll1$ThumbYpos, dataAll1$IndexYpos))
dataAll1$PointZpos <- rowMeans(cbind(dataAll1$ThumbZpos, dataAll1$IndexZpos))
dataAll1$Distance <- round(dataAll$Distance)


####### Define functions####### -------------------------------------------------------
# Euclidean distance
euclideanDist <- function(x1, y1, z1, x2, y2, z2){sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)}

# Function to calculate traveled distance between frames
getDist <- function(x, y, z){
            dist <- rep(as.numeric(NA), length(x)-1) 
             for (i in seq(length(x)-1)){
              dist[i] <- euclideanDist(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1])
 }
  dist <- c(0, dist)
   dist
}



dd <- dataAll1 %>% 
               group_by(SubjectID, NTrial,ExpCondition) %>% 
               mutate(
                      HandPath = getDist(PointXpos, PointYpos, PointZpos)) %>% 
               summarise(
                         Distance = head(Distance, 1),
                         PGA = max(IndexThumbSpan), # Max grip aperture
                         PGAvel = max(IndexThumbSpanVel), # Grip aperture velocit
                         FGA = tail(IndexThumbSpan, 1), # Final grip aperture
                         tPGA = (TimeFromMovOnset[IndexThumbSpan == max(IndexThumbSpan)]) , # Time of peak grip aperture
  )

# Summaries of data for visual exploration -------------------------------------

#Check PGA


ggplot(dd, aes(FGA)) +
  geom_histogram()+ 
  facet_grid(Distance~SubjectID)

ggplot(dd, aes(PGA)) +
  geom_histogram()+ 
  facet_grid(Distance~SubjectID)

ddSummary <- dd %>% 
                   group_by(SubjectID, Distance, ExpCondition) %>% 
                   summarise(
                             sdPGA = sd(PGA),
                             sdFGA = sd(FGA))
  
# What is the standard deviation of the peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdPGA, group = SubjectID)) +
                  geom_line() +
                  geom_point() +
                  facet_wrap(~ExpCondition, ncol = 5) +
                  theme_minimal()

# What is the standard deviation of the final grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdFGA, group = SubjectID)) +
                  geom_line() +
                  geom_point() +
                  facet_wrap(~ExpCondition, ncol = 5) +
                  theme_minimal()

# Full Analysis ### Kinematics

# Import data -------------------------------------------------------------

files <- list.files(path = "./DataCleaned/", full.names = TRUE, pattern = paste(".*", ".csv", sep = "")) # get only files with .csv extension
dataAll <- do.call(rbind, lapply(files, function(x) read_csv(x)))

# Rename columns ----------------------------------------------------

dataAll1 <- dataAll 
 

# Calculate position of midpoint between thumb and index ------------------

dataAll1$PointXpos <- rowMeans(cbind(dataAll1$ThumbXpos, dataAll1$IndexXpos))
dataAll1$PointYpos <- rowMeans(cbind(dataAll1$ThumbYpos, dataAll1$IndexYpos))
dataAll1$PointZpos <- rowMeans(cbind(dataAll1$ThumbZpos, dataAll1$IndexZpos))
dataAll1$Distance  <- round(dataAll$Distance)

# First glance at data ----------------------------------------------------

ggplot(dataAll1) +
  geom_point(aes(ThumbXpos, ThumbYpos), size = .3) +
  geom_point(aes(IndexXpos, IndexYpos), size = .3) +
  coord_fixed() +
  facet_wrap(SubjectID~ ExpCondition + Distance, ncol = 5 )

ggplot(dataAll1) +
  geom_point(aes(WristXpos, WristYpos), size = .3) +
  coord_fixed() +
  facet_wrap(SubjectID~ ExpCondition + Distance, ncol =  5)

ggplot(dataAll1, aes(TimeFromMovOnset, ThumbVel)) +
  geom_point(size = .3) +
  facet_wrap(SubjectID~ ExpCondition + Distance, ncol =  5)

ggplot(dataAll1, aes(TimeFromMovOnset, IndexVel)) +
  geom_point(size = .3) +
  facet_grid(SubjectID~Distance)

ggplot(dataAll1, aes(TimeFromMovOnset, IndexThumbSpan)) +
  geom_point(size = .3) +
  facet_wrap(SubjectID~ ExpCondition + Distance, ncol =  5)

ggplot(dataAll1, aes(WristVel, IndexThumbSpan, colour = TimeFromMovOnset)) +
  geom_point(size = .3) +
  facet_wrap(SubjectID~ ExpCondition + Distance, ncol =  5)

######## Define functions####### --------------------------------------------------------

# Euclidean distance
euclideanDist <- function(x1, y1, z1, x2, y2, z2){sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)}

# Function to calculate traveled distance between frames
getDist <- function(x, y, z){
  dist <- rep(as.numeric(NA), length(x)-1) 
  for (i in seq(length(x)-1)){
    dist[i] <- euclideanDist(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1])
  }
  dist <- c(0, dist)
  dist
}

# Variables of interest ---------------------------------------------------

dd <- dataAll1 %>% 
  group_by(SubjectID, NTrial,ExpCondition) %>% 
  mutate(
    HandPath = getDist(PointXpos, PointYpos, PointZpos)) %>% # here we calculate the distance traveled by the fingers at each frame
    summarise(
    Distance = head(Distance, 1),
    PGA = max(IndexThumbSpan), # Max grip aperture
    PGAvel = max(IndexThumbSpanVel), # Grip aperture velocity
    MaxWristVel = max(WristVel), # Max hand velocity
    FGA = tail(IndexThumbSpan, 1), # Final grip aperture
    MovementTime = tail(TimeFromMovOnset, 1), # Movement time
    MovementLength = sum(HandPath), # Length of the movement along the trajectory
    ThumbXEnd = tail(ThumbXpos, 1), # Position of thumb at contact
    ThumbYEnd = tail(ThumbYpos, 1),
    ThumbZEnd = tail(ThumbZpos, 1),
    IndexXEnd = tail(IndexXpos, 1), # Position of index at contact
    IndexYEnd = tail(IndexYpos, 1),
    IndexZEnd = tail(IndexZpos, 1),
    WristXEnd = tail(WristXpos, 1), # Position of hand marker at contact
    WristYEnd = tail(WristYpos, 1),
    WristZEnd = tail(WristZpos, 1),
    tPGA = (TimeFromMovOnset[IndexThumbSpan == max(IndexThumbSpan)]) , # Time of peak grip aperture
    tMaxThumbVel = (TimeFromMovOnset[ThumbVel == max(ThumbVel)]), # Time of peak thumb velocity
    tMaxIndexVel = (TimeFromMovOnset[IndexVel== max(IndexVel)]), # Time of peak Index velocity
    tMaxWristVel = (TimeFromMovOnset[WristVel== max(WristVel)]), # Time of peak hand velocity
    prop_tPGA = tPGA/MovementTime, # Proportion Time of peak grip aperture
    prop_tMaxThumbVel = tMaxThumbVel/MovementTime, # Proportion Time of peak thumb velocity
    prop_tMaxIndexVel = tMaxIndexVel/MovementTime, # Proportion Time of peak Index velocity
    prop_tMaxWristVel = tMaxWristVel/MovementTime, # Proportion Time of peak hand velocity
    prop_PGA = (sum(HandPath[1:which.max(IndexThumbSpan)])/MovementLength), # Proportion of trajectory at which MGA occurred
    prop_MaxWristVel = (sum(HandPath[1:which.max(WristVel)])/MovementLength) # Proportion of trajectory at which maximum hand velocity occurred

  )

# Overview raw data -------------------------------------------------------

# Top view thumb, index and hand endpoints
ggplot(dd) +
  geom_point(aes(ThumbXEnd, ThumbYEnd), size = .5) +
  geom_point(aes(IndexXEnd, IndexYEnd), size = .5) +
  geom_point(aes(WristXEnd, WristYEnd), size = .5) +
  scale_x_continuous(name = "x-coord.") +
  scale_y_continuous(name = "y-coord.") +
  coord_fixed() +
  facet_wrap(SubjectID~ ExpCondition + Distance, ncol =  5)+
theme_minimal()

# Summaries of Kinematics data for visual exploration --------------------------------

ddSummary <- dd %>% 
  group_by(SubjectID, Distance, ExpCondition) %>% 
  summarise(
    mPGA = mean(PGA),
    mPGAvel = mean(PGAvel),
    mMaxWristVel = mean(MaxWristVel),
    mFGA = mean(FGA),
    mMovementTime = mean(MovementTime),
    mMovementLength = mean(MovementLength),
    mThumbXEnd = mean(ThumbXEnd),
    mThumbYEnd = mean(ThumbYEnd),
    mThumbZEnd = mean(ThumbZEnd),
    mIndexXEnd = mean(IndexXEnd),
    mIndexYEnd = mean(IndexYEnd),
    mIndexZEnd = mean(IndexZEnd),
    mtPGA = mean(tPGA),
    mtMaxThumbVel = mean(tMaxThumbVel),
    mtMaxIndexVel = mean(tMaxIndexVel),
    mtMaxWristVel = mean(tMaxWristVel),
    mprop_tPGA = mean(prop_tPGA),
    mprop_tMaxThumbVel = mean(prop_tMaxThumbVel),
    mprop_tMaxIndexVel = mean(prop_tMaxIndexVel),
    mprop_tMaxWristVel = mean(prop_tMaxWristVel),
    mprop_PGA = mean(prop_PGA),
    mprop_MaxWristVel = mean(prop_MaxWristVel),
    sdPGA = sd(PGA),
    sdFGA = sd(FGA),
    sdThumbXEnd = sd(ThumbXEnd),
    sdThumbYEnd = sd(ThumbYEnd),
    sdThumbZEnd = sd(ThumbZEnd),
    sdIndexXEnd = sd(IndexXEnd),
    sdIndexYEnd = sd(IndexYEnd),
    sdIndexZEnd = sd(IndexZEnd),
    sdThumbCentroid = sqrt(sum((ThumbXEnd - mean(ThumbXEnd))^2 + (ThumbYEnd - mean(ThumbYEnd))^2)/(n())), # standard deviation from centroid
    sdIndexCentroid = sqrt(sum((IndexXEnd - mean(IndexXEnd))^2 + (IndexYEnd - mean(IndexYEnd))^2)/(n())), # standard deviation from centroid
    corThumbIndexXEnd = cor(ThumbXEnd, IndexXEnd),
    corThumbIndexYEnd = cor(ThumbYEnd, IndexYEnd),
    pcorThumbIndexWristYEnd = ppcor::pcor(data.frame(ThumbYEnd, IndexYEnd, WristYEnd), method = "pearson")$estimate[2, 1], # partial correlation
    pcorThumbIndexWristXEnd = ppcor::pcor(data.frame(ThumbXEnd, IndexXEnd, WristXEnd), method = "pearson")$estimate[2, 1] # partial correlatio
  )



# What is the peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, mPGA, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, PGA), width = 1, colour = "gray70") +
  geom_abline(aes(intercept = 0, slope = 1), linetype = "dotted") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

ggplot(ddSummary, aes(Distance, mPGAvel, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, PGAvel), width = 1, colour = "gray70") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the final grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, mFGA, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, FGA), width = 1, colour = "gray70") +
  geom_abline(aes(intercept = 0, slope = 1), linetype = "dotted") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# Relation between peak and final grip apertures
ggplot(ddSummary, aes(mPGA, mFGA, group = SubjectID)) +
  geom_abline(aes(intercept = 0, slope = 1), linetype = "dotted") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the max hand velocity as a function of size?
ggplot(ddSummary, aes(Distance, mMaxWristVel, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, MaxWristVel), width = 1, colour = "gray70") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# # What is the movement time as a function of size?
ggplot(ddSummary, aes(Distance, mMovementTime, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, MovementTime), width = 1, colour = "gray70") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the movement length as a function of size?
ggplot(ddSummary, aes(Distance, mMovementLength, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, MovementLength), width = 1, colour = "gray70") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the time of peak grip aperture as a function of size? 
ggplot(ddSummary, aes(Distance, mtPGA, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, tPGA), width = 1, colour = "gray70") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# At what proportion of the movement did the peak grip aperture occur as a function of size? 
ggplot(ddSummary, aes(Distance, mprop_PGA, group = SubjectID)) +
  geom_jitter(data = dd, aes(Distance, prop_PGA), width = 1, colour = "gray70") +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the standard deviation of the peak grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdPGA, group = SubjectID)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the standard deviation of the final grip aperture as a function of size?
ggplot(ddSummary, aes(Distance, sdFGA, group = SubjectID)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the standard deviation of the thumb endpoint in y as a function of size?
ggplot(ddSummary, aes(Distance, sdThumbYEnd, group = SubjectID)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the standard deviation of the index endpoint in y as a function of size?
ggplot(ddSummary, aes(Distance, sdIndexYEnd, group = SubjectID)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# Are standard deviations in x and y similar for thumb?
ggplot(ddSummary) +
  geom_line(aes(Distance, sdThumbXEnd), colour = "red") +
  geom_line(aes(Distance, sdThumbYEnd), colour = "orange") +
  scale_y_continuous(name = "SD fingers") + 
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# Are standard deviations in x and y similar for index?
ggplot(ddSummary) +
  geom_line(aes(Distance, sdIndexXEnd), colour = "red") +
  geom_line(aes(Distance, sdIndexYEnd), colour = "orange") +
  scale_y_continuous(name = "SD fingers") + 
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# Are standard deviations in x similar for thumb and index?
ggplot(ddSummary) +
  geom_line(aes(Distance, sdThumbXEnd), colour = "red") +
  geom_line(aes(Distance, sdIndexXEnd), colour = "orange") +
  scale_y_continuous(name = "SD fingers") + 
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# Are standard deviations in y similar for thumb and index?
ggplot(ddSummary) +
  geom_line(aes(Distance, sdThumbYEnd), colour = "red") +
  geom_line(aes(Distance, sdIndexYEnd), colour = "orange") +
  scale_y_continuous(name = "SD fingers") + 
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the standard deviation of the thumb endpoint from centroid as a function of size?
ggplot(ddSummary, aes(Distance, sdThumbCentroid, group = SubjectID)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the standard deviation of the index endpoint from centroid as a function of size?
ggplot(ddSummary, aes(Distance, sdIndexCentroid, group = SubjectID)) +
  geom_line() +
  geom_point() +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the correlation between final endpoints of thumb and index in x direction?
ggplot(ddSummary, aes(Distance, corThumbIndexXEnd, group = SubjectID)) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  geom_line() +
  geom_point() +
  scale_y_continuous(limits = c(.90, 1)) +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

# What is the correlation between final endpoints of thumb and index in y direction?
ggplot(ddSummary, aes(Distance, corThumbIndexYEnd, group = SubjectID)) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  geom_line() +
  geom_point() +
  scale_y_continuous(limits = c(-1, 1)) +
  facet_wrap(~ExpCondition, ncol = 5) +
  theme_minimal()

Touchscreen

#upload files
files <- list.files(path = "./DataCleaned/", full.names = TRUE, pattern = paste(".*", ".txt", sep = "")) 
dataT <- do.call(rbind, lapply(files, function(x) read.table(x,header = TRUE)))
raw <-dim(dataT)[1]


#Data Cleaning

#Remove missing trial
dat1 <- dataT %>% 
  filter(ThumbX > 1,IndexX > 1, ThumbX > 1,  IndexY>1, ThumbY>1,  RTs <= 1.5,  RTs > .2)
filtered<- dim(dat1)[1]

#Trials left
1 - (filtered/raw)
## [1] 0.05625
# Euclidean distance
euclideanDist <- function(x1, y1, x2, y2){sqrt((x2 - x1)^2 + (y2 - y1)^2)}


# Filter out bad trials ---------------------------------------------------

ggplot(dat1) +
  geom_point(aes(ThumbXinMM, ThumbYinMM, colour = ExpCondition)) +
  geom_point(aes(IndexXinMM, IndexYinMM, colour = ExpCondition)) +
  geom_segment(aes(x = ThumbXinMM, y = ThumbYinMM, xend = IndexXinMM, yend = IndexYinMM), size = .1) +
  coord_fixed() +
  facet_wrap(Distance~SubjectID, ncol = 2)

# Some touches were wrongly detected (distance between fingers was very small), here we eliminate those trials
dataAll2 <- dat1 %>% 
  mutate(
    FGA = euclideanDist(ThumbXinMM, ThumbYinMM, IndexXinMM, IndexYinMM)
  )

ggplot(dataAll2, aes(FGA)) +
  geom_histogram() +
  facet_grid(Distance~SubjectID)

dataAll3 <- dataAll2 %>% 
  filter(FGA < Distance +30,FGA > Distance -30 )

ggplot(dataAll3, aes(FGA)) +
  geom_histogram()+ 
  facet_grid(Distance~SubjectID)

# Some trials were much slower than others, here we eliminate all trials with Time > 1.2 s. We might need a more elengate way for this step.
ggplot(dataAll3, aes(RTs)) +
  geom_histogram() +
  geom_vline(aes(xintercept = 1.2)) +
  facet_grid(Distance~SubjectID)

dataAll4 <- dataAll3 %>% 
  filter(RTs <= 1.2)

ggplot(dataAll4, aes(RTs)) +
  geom_histogram() +
  geom_vline(aes(xintercept = 1.2)) +
  facet_grid(Distance~SubjectID)

# Trials eliminated:
length(dataT$Distance) - length(dataAll4$Distance) # number
## [1] 41
(length(dataT$Distance) - length(dataAll4$Distance))/length(dataT$Distance)*100 # percentage
## [1] 8.541667
# Calculate position of midpoint between thumb and index ------------------

dataAll4$MidpointX <- rowMeans(cbind(dataAll4$ThumbXinMM, dataAll4$IndexXinMM))
dataAll4$MidpointY <- rowMeans(cbind(dataAll4$ThumbYinMM, dataAll4$IndexYinMM))


#First touch
dataAll4$ThumbIndexTimeDiff<-dataAll4$IndexTime - dataAll4$ThumbTime

# Cleanup dataframe -------------------------------------------------------




# First look at raw data --------------------------------------------------


# FGA
ggplot(dataAll4, aes(Distance, FGA, group = ExpCondition)) +
  geom_abline(intercept = 0, slope = 1, linetype = "dotted") +
  geom_jitter(width = 2) +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(~ExpCondition)

# x coordinates thumb and index
ggplot(dataAll4, aes(ThumbXinMM, IndexXinMM, group = Distance, colour = factor(Distance))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  coord_fixed() +
  facet_grid(SubjectID~ExpCondition)

# y coordinates thumb and index
ggplot(dataAll4, aes(ThumbYinMM, IndexYinMM, group = Distance, colour = factor(Distance))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  coord_fixed() +
  facet_grid(SubjectID~ExpCondition)

# Thumb with respect to target
ggplot(dataAll4, aes(TargetThumbXInMM - ThumbXinMM, TargetThumbYInMM - ThumbYinMM)) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  geom_point() +
  coord_fixed() +
  facet_grid(SubjectID~ExpCondition)

# Index with respect to target
a<-ggplot(dataAll4, aes(TargetIndexXInMM - IndexXinMM, TargetIndexYInMM - IndexYinMM)) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  geom_point() +
  coord_fixed() +
  facet_grid(SubjectID~ExpCondition)

# Index and Thumb with respect to target
b<-ggplot(dataAll4, aes(TargetThumbXInMM - ThumbXinMM, TargetThumbYInMM - ThumbYinMM, )) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  geom_point() +
  coord_fixed() +
  facet_grid(SubjectID~ExpCondition)


grid.arrange(b,a, ncol = 2)

# Midpoint between thumb and index
ggplot(dataAll4, aes(MidpointX, MidpointY)) +
  geom_point() +
  facet_grid(SubjectID~ExpCondition)

# Time
ggplot(dataAll4, aes(Distance, RTs)) +
  geom_jitter(width = 2) +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(SubjectID~ExpCondition)

# Which finger first?
ggplot(dataAll4, aes(ThumbIndexTimeDiff)) +
  geom_vline(xintercept = 0, linetype = "dotted") +
  geom_histogram() +
  facet_grid(SubjectID~ExpCondition)

# Summarize data (Subject, Condition, Distance) ---------------------------

dd_agg <- dataAll4 %>% 
  group_by(ExpCondition, Distance,SubjectID) %>% 
  summarise(
    mTargetThumbX = mean(TargetThumbXInMM),
    mTargetThumbY = mean(TargetThumbYInMM),
    mTargetIndexX = mean(TargetIndexXInMM),
    mTargetIndexY = mean(TargetIndexYInMM),
    mThumbX = mean(ThumbXinMM),
    mThumbY = mean(ThumbYinMM),
    mIndexX = mean(IndexXinMM),
    mIndexY = mean(IndexYinMM),
    mRTs = mean(RTs),
    mThumbIndexTimeDiff = mean(ThumbIndexTimeDiff),
    mFGA = mean(FGA),
    mMidpointX = mean(MidpointX),
    mMidpointY = mean(MidpointY),
   
    sdThumbX = sd(ThumbXinMM),
    sdThumbY = sd(ThumbYinMM),
    sdIndexX = sd(IndexXinMM),
    sdIndexY = sd(IndexYinMM),
    sdFGA = sd(FGA),
    sdMidpointX = sd(MidpointX),
    sdMidpointY = sd(MidpointY),
    corThumbIndexX = cor(ThumbXinMM, IndexXinMM),
    corThumbIndexY = cor(ThumbYinMM, IndexYinMM)
    #corFirstTouch = cor(ThumbIndexTimeDiff, sd(fga))
  )


ggplot(dd_agg, aes(Distance, mFGA)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point() +
  facet_grid(SubjectID~ExpCondition)

ggplot(dd_agg, aes(Distance, mTargetThumbY - mThumbY)) +
  geom_point() +
  facet_grid(SubjectID~ExpCondition)

ggplot(dd_agg, aes(Distance, mTargetThumbX - mThumbX)) +
  geom_point() +
  facet_grid(SubjectID~ExpCondition)

ggplot(dd_agg, aes(Distance, sdFGA)) +
  geom_line() +
  facet_grid(SubjectID~ExpCondition)