diff --git a/R/check_ppt_n.R b/R/check_ppt_n.R index 343487d..253975d 100644 --- a/R/check_ppt_n.R +++ b/R/check_ppt_n.R @@ -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 diff --git a/R/plot_spatial.R b/R/plot_spatial.R index 0935ea7..1cd867f 100644 --- a/R/plot_spatial.R +++ b/R/plot_spatial.R @@ -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) @@ -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() @@ -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, @@ -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")), @@ -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 diff --git a/docs/404.html b/docs/404.html index c159b2b..f9ea8bf 100644 --- a/docs/404.html +++ b/docs/404.html @@ -17,7 +17,6 @@ - - - - - - - -Animating eyetools data • eyetools - - - - - - - - - - - - - - - - - Skip to contents - - -
- - - - -
-
- - - -
-library(eyetools)
-library(gganimate)
-#> Warning: package 'gganimate' was built under R version 4.2.3
-#> Loading required package: ggplot2
-#> Warning: package 'ggplot2' was built under R version 4.2.3
-library(tidyverse)
-#> ── Attaching packages ─────────────────────────────────────── tidyverse
-#> 1.3.2 ──
-#>  tibble  3.2.1      dplyr   1.1.2
-#>  tidyr   1.3.0      stringr 1.5.0
-#>  readr   2.1.3      forcats 0.5.2
-#>  purrr   1.0.2     
-#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
-#>  dplyr::filter() masks stats::filter()
-#>  dplyr::lag()    masks stats::lag()
-

In this article, we offer examples of how eyetools data can be used -with the gganimate package.

-
-

Plotting raw data with lag -

-

This animation takes raw data (of a single trial), transforms it into -a larger dataset with plot-specific parameters, and then provides an -animation of the participant’s gaze. The lag feature highlights the path -taken and slowly fades to allow more data to be presented

-
-
-
-data <- example_counterbalance[example_counterbalance$trial == 3,] |> # take a random trial
-  interpolate() |>
-  smoother() |>
-  mutate(id = seq(1, n()), # add an ID column to original dataset,
-         type = "original",
-         size = 10, # specify initial size
-         alpha = 1) |> # specify initial alpha
-  mutate(time = time - min(time))
-
-#bind the original dataset to a mid and end state too
-data_2 <- rbind(data,
-                data |> # create second version of the dataset - this is the tail
-                  mutate(type = "mid_state",      
-                         time = time + 45,         
-                         size = size * 0.5,        
-                         alpha = alpha * 0.2),     
-                
-                data |>                           
-                  mutate(type = "end_state",      
-                         time = time + 300, # this determines the length of the tail and the final state   
-                         alpha = 0))            
-
-# pass this combined dataset to ggplot & animate
-plot_animate <- data_2 |>
-  ggplot(aes(x = x, y = y, group = id,
-             size = size, alpha = alpha)) +
-  # add a background image
-  annotation_raster(magick::image_read("../data/HCL_sample_image.jpg"),
-                    xmin = 0,
-                    xmax = 1920,
-                    ymin = 0,
-                    ymax = 1080) +
-  geom_point(colour = "red") +
-  transition_components(time) +
-  scale_size_identity() +
-  scale_alpha_identity() +
-  lims(x = c(0, 1920), y = c(0, 1080))
-
-animate(plot_animate,
-        duration = 14)
-

-
-
-## To save an animation
-#anim_save("point_lag.gif", plot_animate, height = 1080, width = 1920, duration = round(max(data_2$time)/1000),
-#          end_pause = 5)
-
-
-

Plotting AOI entries over raw data -

-
-#data <- example_raw_WM |> 
-#  interpolate() |> 
-#  smoother()
-#
-#AOIs_WM
-#
-#data_plot <- data |> 
-#  filter(trial == 1) |> 
-#  mutate(in_AOI = case_when(between(x, AOIs_WM$x[1], AOIs_WM$x[1] + AOIs_WM$width_radius[1]) ~ TRUE,
-#                            between(x, AOIs_WM$x[2], AOIs_WM$x[2] + AOIs_WM$width_radius[1]) ~ TRUE,
-#                            between(x, AOIs_WM$x[3], AOIs_WM$x[3] + AOIs_WM$width_radius[1]) ~ TRUE,
-#                            between(x, AOIs_WM$x[4], AOIs_WM$x[4] + AOIs_WM$width_radius[1]) ~ TRUE,
-#                            .default = FALSE)) |> 
-#    mutate(id = seq(1, n()), # add an ID column to original dataset,
-#         type = "original",
-#         size = 10, # specify initial size
-#         alpha = 1) |> # specify initial alpha
-#  mutate(time = time - min(time))
-#
-#
-#plot_animate <-  data_plot |>
-#  ggplot(aes(x = x, y = y,
-#             colour = in_AOI
-#             )) +
-#  geom_point() +
-#  ggtitle("{frame_time}")# + # optional; added to illustrate frame time explicitly
-#  transition_components(time)
-#
-#animate(plot_animate, duration = round(max(data_plot$time)/100))
-
-
-
- - - - -
- - - - - - - diff --git a/docs/articles/Animating-eyetools-data_files/figure-html/unnamed-chunk-2-1 2.gif b/docs/articles/Animating-eyetools-data_files/figure-html/unnamed-chunk-2-1 2.gif deleted file mode 100644 index 2bb9dd8..0000000 Binary files a/docs/articles/Animating-eyetools-data_files/figure-html/unnamed-chunk-2-1 2.gif and /dev/null differ diff --git a/docs/articles/Animating-eyetools-data_files/figure-html/unnamed-chunk-2-1.gif b/docs/articles/Animating-eyetools-data_files/figure-html/unnamed-chunk-2-1.gif deleted file mode 100644 index 2bb9dd8..0000000 Binary files a/docs/articles/Animating-eyetools-data_files/figure-html/unnamed-chunk-2-1.gif and /dev/null differ diff --git a/docs/articles/analysis_steps.html b/docs/articles/analysis_steps.html deleted file mode 100644 index 1413ee2..0000000 --- a/docs/articles/analysis_steps.html +++ /dev/null @@ -1,110 +0,0 @@ - - - - - - - - -analysis_steps • eyetools - - - - - - - - - - Skip to contents - - -
- - - - -
-
- - - -
-<<<<<<< HEAD
-library(eyetools)
-======= -library(eyetools)
->>>>>>> 2c885122466b442f37615357c203d6a19a203cc7 - -
- - - - - - - - - - - - diff --git a/docs/articles/articles/Animating-eyetools-data.html b/docs/articles/articles/Animating-eyetools-data.html deleted file mode 100644 index cc8a2bf..0000000 --- a/docs/articles/articles/Animating-eyetools-data.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/docs/articles/eyetools.html b/docs/articles/eyetools.html index 1bf8e37..62177c0 100644 --- a/docs/articles/eyetools.html +++ b/docs/articles/eyetools.html @@ -19,7 +19,6 @@ - Articles • eyetoolsArticles • eyetools @@ -10,7 +10,7 @@ eyetools - 0.6.1 + 0.7.0 - - - - - -
- - - - -
-
- - - -
-

Steps -

-
    -
  1. Make a branch from the master
  2. -
  3. Make changes to package components (scripts, data, etc)
  4. -
  5. Use load_all() to make the updates -available for use.
  6. -
  7. Use document() to update the function -documentation and NAMESPACE
  8. -
  9. Use check() to make sure there are no errors, correct -any warnings, etc.
  10. -
  11. Commit to github.
  12. -
-
-
-
- - - - -
- - - - - - - diff --git a/docs/authors.html b/docs/authors.html index 212f69d..b33620e 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -1,9 +1,5 @@ -<<<<<<< HEAD -Authors and Citation • eyetoolsAuthors and Citation • eyetoolsAuthors and Citation • eyetools @@ -14,11 +10,7 @@ eyetools -<<<<<<< HEAD - 0.4.3 -======= - 0.5.0 ->>>>>>> 2c885122466b442f37615357c203d6a19a203cc7 + 0.7.0