0

I want to build several plots from one large database, so that I have one plot for each Text (factor) and for each Measure (the many resulting measures of an eye tracking study). The following is a much simpler example of what I am trying to to:

Let's say this is my dataset

Text <- c(1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2)
Position <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
Modified <- c(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)
Line_on_page <- c(1, 1, 1, 1, 2,2,2,2 ,1 ,1,1,1,2,2,2,2)
IA_FIXATION_DURATION <- c(250.3, 70.82, 400, 120.12, 270, 120.5, 100.54, 212.43, 250.3, 70.82, 320.29, 123.12, 260, 121.5, 100.54, 272.43)
IA_FIXATION_COUNT <- c(1,0,1,1,3,2,0, 1, 1,0,1,2,3,2,0, 2)
IA_LABEL <- c("she", "did", "not", "know", "what", "to", "say", "to", "she", "did", "not", "know", "what", "to", "do", "to")
testDF <- data.frame(Text , Position , Line_on_page, Modified, IA_FIXATION_DURATION, IA_FIXATION_COUNT, IA_LABEL)

so I want a heatmap (or another graph) for each Text (1/2/3), and for each measure (IA_FIXATION_DURATION/IA_FIXATION_COUNT)

# so first i create my vectors


library(stringr)
library(reshape2)
library(ggplot2)
library(ggthemes)
library(tidyverse)

Text_list <- unique(testDF$Text)
Measure_list <- testDF %>% dplyr::select_if(is.numeric) %>% colnames() %>% as.vector()

# create graphing function
Heatmap_FN <- function(testDF, na.rm = TRUE, ...){

  # create for loop to produce ggplot2 graphs 
  for (i in seq_along(Text_list)) { 
    for (j in seq_along(Measure_list)) {

      # create plot for each text in dataset 
      plots <- ggplot(subset(testDF, testDF$Text==Text_list[i])) +
        geom_tile(aes(x=Position, 
                      y=Line_on_page, 
                      fill = Measure_list[j])) +
        geom_text(aes(x=Position, 
                      y=Line_on_page, 
                      label=IA_LABEL),
                  color = "white", size = 2, family = "sans") +
        scale_fill_viridis_c(option = "C", na.value = "black") +
        scale_y_reverse() +
        facet_grid(Page ~ Modified)+
        theme(legend.position = "bottom") + 
        ggtitle(paste(Text_list[i],j, 'Text \n'))

      ggsave(plots, file=paste(Measure_list[j], "_T", Text_list[i], ".pdf", sep = ""), height = 8.27, width = 11.69, units = c("in"))

    }
  }
}

Heatmap_FN(testDF)

now, I am pretty sure that the problem lies in the geom_tile "fill" part, where I would like to indicate to the function that I want to use the results variables one by one to produce the plot.

Any ideas on how to fix that? Thanks

4
  • 1
    These might help stackoverflow.com/a/50522928/786542 & stackoverflow.com/a/52045111/786542 Commented Mar 9, 2019 at 10:53
  • I can't quite follow what you're after, but I think you've made this more convoluted than it needs to be. You've got a pair of nested for loops and are using facet.grid in the function. Can you specify exactly which combinations of which features you want to use to subset the data, and then what you want to plot in that subset? Commented Mar 9, 2019 at 11:31
  • Hi and thanks for your answer. I need a plot for each text, for each measure, where I can see the difference between the two possible versions (my ‘fixed factor’, ie, ‘modified 0/1’), more specifically with modified and not-modified items aside for a clear visual comparison (therefore the use of facet grid). Does that affect the function? It worked nicely when I manually inserted the measure to use as ‘fill’. However, I have many measure to observe and wanted to loop them all, if possible. Thought that a vector could work Commented Mar 10, 2019 at 3:13
  • many thanks to you all! I changed the following lines and my code is now working: (j in seq_along(Measure_list)) was turned into (j in Measure_list) and fill = Measure_list[j]) into fill = get(j) Commented Mar 11, 2019 at 9:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.