Skip to content

Commit

Permalink
Merge pull request #8 from tombeesley/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tombeesley authored Oct 19, 2024
2 parents 2e2a332 + 09bae27 commit 53f089c
Show file tree
Hide file tree
Showing 272 changed files with 94,163 additions and 20,901 deletions.
1 change: 0 additions & 1 deletion R/check_ppt_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
time_rep <- duplicated(rle(data$time)$values)

if (sum(trial_rep) > 0) stop("multiple duplicated trials detected. Have you forgotten to specify the participant_ID?")
if (sum(time_rep) > 0) stop("multiple duplicated timepoints detected. Have you forgotten to specify the participant_ID?")

participant_ID = "participant_ID"
data <- cbind(data, participant_ID = c("NOT A VALID ID")) # just assign a value
Expand Down
86 changes: 53 additions & 33 deletions R/plot_spatial.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#' Plot raw data and fixations
#'
#' A tool for visualising raw eye-data, processed fixations, and saccades. Can use all three data types. Fixations can be labeled
#' A tool for visualising raw eye-data, processed fixations, and saccades. Can use all three data types together and independently. Fixations can be labeled
#' in the order they were made. Can overlay areas of interest (AOIs) and customise the resolution.
#'
#' @param data A dataframe of either fixation data (from fix_dispersion) or raw data
#' @param data_type Whether data is a fixation ("fix") raw data ("raw"), or saccade ("sac")
#' @param raw_data data in standard raw data form (time, x, y, trial)
#' @param fix_data data output from fixation function
#' @param sac_data data output from saccade function
#' @param AOIs A dataframe of areas of interest (AOIs), with one row per AOI (x, y, width_radius, height). If using circular AOIs, then the 3rd column is used for the radius and the height should be set to NA.
#' @param trial_number can be used to select particular trials within the data
#' @param bg_image The filepath of an image to be added to the plot, for example to show a screenshot of the task.
#' @param res resolution of the display to be shown, as a vector (xmin, xmax, ymin, ymax)
#' @param flip_y reverse the y axis coordinates (useful if origin is top of the screen)
Expand All @@ -17,32 +19,33 @@
#'
#' @examples
#' data <- combine_eyes(HCL)
#'
#' data <- data[data$pNum == 118,]
#' # plot the raw data
#' plot_spatial(data = data[data$pNum == 118,], data_type = "raw")
#' plot_spatial(raw_data = data[data$pNum == 118,])
#'
#' # plot both raw and fixation data together
#' plot_spatial(raw_data = data, fix_data = fixation_dispersion(data))
#'
#' # add in AOIs
#' plot_spatial(data = data[data$pNum == 118,], data_type = "raw", AOIs = HCL_AOIs)
#' #plot one trial
#' plot_spatial(raw_data = data, fix_data = fixation_dispersion(data), trial_number = 1)
#'
#' @import ggplot2
#' @import ggforce
#' @importFrom magick image_read
#'

plot_spatial <- function(data = NULL,
data_type = NULL,
plot_spatial <- function(raw_data = NULL,
fix_data = NULL,
sac_data = NULL,
AOIs = NULL,
trial_number = NULL,
bg_image = NULL,
res = c(0,1920,0,1080),
flip_y = FALSE,
show_fix_order = TRUE,
plot_header = FALSE) {

if (is.null(data_type) == TRUE) {
# input data for both fixations and raw data
stop("Type of data not specified. Use `data_type = 'fix'` for fixations `data_type = 'raw'` for raw data, or 'sac' for saccades")

}
if(!is.null(trial_number) & !is.numeric(trial_number)) stop("trial_number input expected as numeric values")

final_g <- ggplot()

Expand All @@ -53,30 +56,42 @@ plot_spatial <- function(data = NULL,
if (is.null(AOIs)==FALSE) final_g <- add_AOIs(AOIs, final_g)

# add raw data
if (is.null(raw_data)==FALSE) {

if(!is.null(trial_number)) {
raw_data <- raw_data[raw_data$trial %in% trial_number,]
if(nrow(raw_data) == 0) stop("no trial found for raw data. Check the data has the trials")
}

if (data_type == "raw") final_g <- add_raw(data, final_g)
final_g <- add_raw(raw_data, final_g)
}

# PLOT FIXATION DATA
if (data_type == "fix") {
if (is.null(fix_data)==FALSE) {

if(!is.null(trial_number)) {
fix_data <- fix_data[fix_data$trial %in% trial_number,]
if(nrow(fix_data) == 0) stop("no trial found for fixation data. Check the data has the trials")
}

data$fix_n <- seq_len(nrow(data))
x <- data$x
y <- data$y
disp_tol <- data$disp_tol
duration <- data$duration
fix_n <- data$fix_n
fix_data$fix_n <- seq_len(nrow(fix_data))

x <- fix_data$x
y <- fix_data$y
disp_tol <- fix_data$disp_tol
duration <- fix_data$duration
fix_n <- fix_data$fix_n

final_g <-
final_g +
geom_circle(data = data,
aes(x0 = x, y0 = y, r = disp_tol/2, fill = duration),
alpha = .2)
geom_circle(data = fix_data,
aes(x0 = x, y0 = y, r = disp_tol/2, fill = duration),
alpha = .2)
if (show_fix_order == TRUE) {

final_g <-
final_g +
geom_label(data = data,
geom_label(data = fix_data,
aes(x = x, y = y, label = fix_n),
hjust = 1,
vjust = 1,
Expand All @@ -88,16 +103,21 @@ plot_spatial <- function(data = NULL,
}

# PLOT SACCADE DATA
if (data_type == "sac"){
if (is.null(sac_data)==FALSE){

origin_x <- data$origin_x
origin_y <- data$origin_y
terminal_x <- data$terminal_x
terminal_y <- data$terminal_y
if(!is.null(trial_number)) {
sac_data <- sac_data[sac_data$trial %in% trial_number,]
if(nrow(sac_data) == 0) stop("no trial found for saccade data. Check the data has the trials")
}

origin_x <- sac_data$origin_x
origin_y <- sac_data$origin_y
terminal_x <- sac_data$terminal_x
terminal_y <- sac_data$terminal_y

final_g <-
final_g +
geom_segment(data = data,
geom_segment(data = sac_data,
aes(x = origin_x, y = origin_y, xend = terminal_x, yend = terminal_y),
colour = "blue",
arrow = arrow(length = unit(0.5, "cm")),
Expand Down Expand Up @@ -169,7 +189,7 @@ add_raw <- function(dataIn, ggplot_in){
size = 1,
na.rm = TRUE)

return(ggplot_in)
return(ggplot_in)
}

# function to add AOIs
Expand Down
11 changes: 2 additions & 9 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed docs/Beesley_2015_ss.jpg
Binary file not shown.
Loading

0 comments on commit 53f089c

Please sign in to comment.