Creative Commons License R.Muralikrishnan, MPI for Empirical Aesthetics. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Licence.
# #####################################################################O
# R Script to plot intensity vs time in various forms
# Bar Plots, Balloon Plots, Facetted Plots etc.
#
# Tidy Version ... 02.05.2017
# This script is called from a wrapper shell script that replaces
# the pattern %%INFILE%% with the input excel file name
#
# Author: R.Muralikrishnan
# #####################################################################m

library(tidyverse)
# setwd(".......")

# Read the input file name from the input from IntensityPlot-Tidy.sh
InputFile <- "%%INFILE%%"

Rhythm <- substr(InputFile, 11, 12)

# TSV file of the wrangled / mutated data that would be used for plotting
Output0 <- paste("Out-RhythmData__", Rhythm, ".txt", sep = "")
# Plots
Output1 <- paste("Plot-Balloon-Intensity_AdjustedTime__", Rhythm, ".pdf", sep = "")
Output2 <- paste("Plot-Line-IOIDiff_Facetted__", Rhythm, ".pdf", sep = "")
Output3 <- paste("Plot-Line-Intensity_Facetted__", Rhythm, ".pdf", sep = "")
Output4 <- paste("Plot-Balloon-Intensity_TimeNorm__", Rhythm, ".pdf", sep = "")

###

# The input data is an excel sheet, with the following header. 
# (Note: NO_SPACES in field names!!!)
# Participant   Time   Intensity   Note   IOI   IOI_norm   Time_norm   Intensity_norm    Note_Value_Intended

###

# Read the input data from the excel sheet
readxl::read_excel(InputFile, col_names = TRUE, na = "NA") %>%
  # Group by Participants
  group_by(Participant) %>% 
  # Add new column : TimeBegin = First instance of Time for a given participant
  # Time[1] would just be the value from the first row...but we need it on a per-Participant basis!
  # Found 'first' from here: http://stackoverflow.com/questions/29152683/r-dplyr-tidyr-mutate-new-columns-with-data-from-other-observations
  mutate(TimeBegin = first(Time)) %>%
  # Add new column : AdjustedTime
  mutate(AdjustedTime = Time - TimeBegin) %>%
  # Group by Participants and Note_Value_Intended ...
  group_by(Participant, Note_Value_Intended) %>%
  # ... for calculating the IOI_mean/Participant/Note_Value
  mutate(IOI_mean = mean(IOI)) %>%
  # Add new column : IOI_diff
  mutate(IOI_diff = ifelse(is.na(IOI_mean), NA, IOI - IOI_mean)) %>%
  # Add new column : DiffDirection
  mutate(DiffDirection = ifelse(IOI_diff >= 0, "POS", "NEG")) %>%
  # Group by Participants ...
  group_by(Participant) %>%
  # ... for calculating Cumulative_TimeN
  mutate(Cumulative_TimeN = cumsum(ifelse(is.na(IOI_mean), 0, IOI_mean))) -> RhythmData

# WRONG: mutate(Cumulative_TimeN = ifelse(is.na(IOI_mean), 0, cumsum(na.omit(IOI_mean))))

# Note: The total IOI_means per participant = last entry of AdjustedTime for that Participant,
# because, well the IOI_means were calculated based on existing IOIs, and just 
# equally distributed over the course of the rhythm (because they are means)

# http://stackoverflow.com/questions/22337394/combine-mutate-with-conditional-values

# Write the Wrangled data to disk
write_tsv(RhythmData, Output0)


# Generate plots 

cairo_pdf(Output1, onefile=TRUE, width=12, height=7)

# geom_point(shape=21, colour="skyblue3") + scale_size(guide="none") +

p <- ggplot(data = RhythmData, aes(x=AdjustedTime,y=Participant,size=Intensity,fill=Intensity)) + 
  geom_point(shape=23, colour="skyblue3") + scale_size(guide="none") +
  scale_fill_gradientn(colours = c("lightblue", "steelblue", "midnightblue")) +
  theme_grey(base_size=8, base_family="Verdana") + 
  theme(panel.grid.minor=element_blank(), 
        panel.grid.major.x=element_line(colour="white",size=0.1), 
        panel.grid.major.y=element_line(colour="white",size=0.1))

p + geom_point(aes(x=Cumulative_TimeN,y=Participant), 
               shape="|", colour="peru", size=7)


dev.off()

cairo_pdf(Output2, onefile=TRUE, width=12, height=10)

p <- ggplot(data = RhythmData, aes(x=Time_norm, y=((IOI_diff/IOI_mean)*100))) +
  geom_hline(yintercept=0, colour="bisque") + scale_y_continuous(name="Percentage IOI Difference from Mean IOI") +
  geom_line(colour="darkolivegreen") +
  theme_bw(base_size=8, base_family="Verdana") + 
  theme(panel.grid.minor=element_blank(),
        panel.grid.major.x=element_line(colour="white",size=0.1), 
        panel.grid.major.y=element_line(colour="white",size=0.1),
        panel.border=element_rect(colour="lightgrey",size=0.1))

p + facet_grid(Participant ~ . )

dev.off()

cairo_pdf(Output3, onefile=TRUE, width=12, height=10)

p <- ggplot(data = RhythmData, aes(x=Time_norm, y=Intensity)) + 
  geom_area(fill="bisque") +
  geom_line(colour="darkolivegreen") +
  theme_bw(base_size=8, base_family="Verdana") + 
  theme(panel.grid.minor=element_blank(),
        panel.grid.major.x=element_line(colour="white",size=0.1), 
        panel.grid.major.y=element_line(colour="white",size=0.1),
        panel.border=element_rect(colour="lightgrey",size=0.1))

p + facet_grid(Participant ~ .)

dev.off()


cairo_pdf(Output4, onefile=TRUE, width=10, height=7)


ggplot(data = RhythmData, aes(x=Time_norm,y=Participant,size=Intensity_norm,fill=Intensity_norm)) + 
  geom_point(shape=21, colour="skyblue3") + scale_size(guide="none") +
  scale_fill_gradientn(colours = c("lightblue", "steelblue", "midnightblue")) +
  theme_grey(base_size=8, base_family="Verdana") + 
  theme(panel.grid.minor.x=element_line(colour="darkgoldenrod",size=0.03), 
        panel.grid.minor.y=element_blank(),
        panel.grid.major.x=element_line(colour="darkgoldenrod",size=0.03), 
        panel.grid.major.y=element_line(colour="white",size=0.1))


dev.off()