Creative Commons License R.Muralikrishnan, MPI for Empirical Aesthetics. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Licence.
# #############################################
# R Script to plot intensity vs time
# Bar Plots, Balloon Plots, Facetted Plots etc.
#
## Script is still work in progress ... 27.02.2017
# Author: R.Muralikrishnan
# #############################################


# Libraries required: ggplot2, cairo_pdf
#
# The input data is assumed to be a tab-separated text file, 
# with the following header. (Note: NO_SPACES in field names!!!)
#
# Participant   Time   Intensity   Note   IOI   IOI_norm   Time_norm   Intensity_norm
#

library(ggplot2)


### Rhythm 1

# Read the input data
RhythmDataOrig <- read.table("IntensityData-R1_TimeAdjusted.txt", header=TRUE)

# Calculate the mean IOI per subject per note_type
MeanIOIPerSubjPerNoteType <- aggregate(IOI ~ Participant * Note_Value_Intended, 
                                       data = RhythmDataOrig, 
                                       FUN = mean )



# Integrate the IOI means calculated into the original data frame per subject per note type
# We could do it in awk, but let's try it here itself for once, shall we? :-)

# http://stackoverflow.com/questions/30913338/join-two-data-tables-and-use-only-one-column-from-second-dt-in-r
library(dplyr)

# This is more useful, if the column names are different in the different tables
# rdt1 <- left_join(x = rdt, y = m, by = c("Participant" = "Participant","Note_Value_Intended" = "Note_Value_Intended"))

# Since in our case, the column names are identical, simply the following does the trick...and what a beautiful trick!
RhythmData <- left_join(x = RhythmDataOrig, 
                        y = MeanIOIPerSubjPerNoteType,                        
                        by = c("Participant","Note_Value_Intended"))

# The new table will have: 
# the original IOIs in column IOI.x 
# and the mean IOIs in column IOI.y
# So let's rename them with more meaningful names.

# http://stackoverflow.com/questions/7531868/how-to-rename-a-single-column-in-a-data-frame#16490387
names(RhythmData)[names(RhythmData)=="IOI.x"] <- "IOI"
names(RhythmData)[names(RhythmData)=="IOI.y"] <- "IOI_mean"


# x <- aggregate(IOI_mean ~ Participant, 
#                data = RhythmData, 
#                FUN = sum )

#http://stackoverflow.com/questions/3651651/adding-a-column-to-a-dataframe-in-r
#rhdata$newcol <- apply(rhdata,1,function(V_Current_Row) {as.nurmeric(V_Current_Row[5]) * 2})
#a <- m[m$Participant==b & m$Note_Value_Intended==d,][,3]
# A beautiful explanation of the apply function
#http://www.computerworld.com/article/2486425/business-intelligence/business-intelligence-4-data-wrangling-tasks-in-r-for-advanced-beginners.html?page=2
#rdt$NEW <- apply(rdt,1,function(V_Current_Row) {as.numeric(V_Current_Row[10]) + as.numeric(V_Current_Row[11])})

# Now we need the Cumulative_Time for each row based on the IOI_means in the previous rows
# for that particular participant!
# This is again a simple thing in awk...but after a number of web searches, 
# there's a way to do it in R, as well...using data.table package and the cumsum function.
# http://stackoverflow.com/questions/16741683/conditional-cumulative-sum-in-r#16741878
# http://r-datatable.com

library(data.table)

DT <- data.table(RhythmData)
DT[is.na(IOI_mean), Cumulative_TimeN := 0]
DT[!is.na(IOI_mean), Cumulative_TimeN := cumsum(IOI_mean), by = c('Participant')]

# 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)

# A data.table is apparently almost the same as data.frame, except, 
# data.table does not have row names.  See http://r-datatable.com.
# And apparently much faster. In any case, the name itself looks less cryptic. Wow!

DT[is.na(IOI_mean), IOI_diff := 0]
DT[!is.na(IOI_mean), IOI_diff := IOI - IOI_mean]

DT[(IOI_diff > 0), DiffDirection := "POS"]
DT[(IOI_diff < 0), DiffDirection := "NEG"]

# Everything that we do with data frames can be done with data.tables as well apparently! 
RhythmData <- DT

#write.table(RhythmData,"Intensity-Data-Calculated")

# Balloon Plot
cairo_pdf("Intensity-Balloon-Plot-R1.pdf", onefile=TRUE, width=10, height=7)


ggplot(data = RhythmData, aes(x=Time,y=Participant,size=Intensity,fill=Intensity)) + 
  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=element_blank(), panel.grid.major.x=element_line(colour="white",size=0.1), panel.grid.major.y=element_line(colour="white",size=0.1))

dev.off()

cairo_pdf("Intensity-Balloon-Plot-R1_AdjustedTime.pdf", onefile=TRUE, width=12, height=7)
# 
# ggplot(data = RhythmData,
# aes(x=AdjustedTime,y=Participant,size=Intensity,fill=Intensity)) +
# 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=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 <- 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)

#pp + geom_pointline(aes(x=AdjustedTime,y=IOI_diff))

dev.off()

cairo_pdf("Intensity-Plot-R1_IOIDiff_Facetted.pdf", 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))

#orange seagreen
p + facet_grid(Participant ~ . )

dev.off()

cairo_pdf("Intensity-Plot-R1_Intensity_Facetted.pdf", 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("Intensity-Balloon-Plot-R1_norm1.pdf", 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="red",size=0.03), 
        panel.grid.minor.y=element_blank(),
        panel.grid.major.x=element_line(colour="red",size=0.03), 
        panel.grid.major.y=element_line(colour="white",size=0.1))


dev.off()

cairo_pdf("Intensity-Barplot-R1_norm_S1.pdf", onefile=TRUE, width=10, height=7)

RhythmData_S1 <- RhythmData[RhythmData$Participant=="S01", ]

# Bar Plot / Histogram : Fill colour fixed, with fixed bar width
ggplot(data = RhythmData_S1, aes(x=Time_norm,y=Intensity_norm)) + 
  geom_histogram(stat="identity", width=0.005, position="identity", fill="steelblue") +
  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))

dev.off()



# The plots could be stacked on top of each other using facets;

cairo_pdf("Intensity-Barplot-R1_norm_Faceted.pdf", onefile=TRUE, width=7, height=10)
p <- ggplot(data = RhythmData, aes(x=Time_norm,y=Intensity_norm)) + 
  geom_histogram(stat="identity", width=0.005, position="identity", fill="steelblue") +
  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 + facet_grid(Participant ~ .)

dev.off()



#### Rhythm 4

RhythmData <- read.table("IntensityData-R4.txt", header=TRUE)

# Balloon Plot
cairo_pdf("Intensity-Balloon-Plot-R4.pdf", onefile=TRUE, width=10, height=7)


ggplot(data = RhythmData, aes(x=Time,y=Participant,size=Intensity,fill=Intensity)) + 
  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=element_blank(), panel.grid.major.x=element_line(colour="white",size=0.1), panel.grid.major.y=element_line(colour="white",size=0.1))

dev.off()


cairo_pdf("Intensity-Balloon-Plot-R4_norm.pdf", 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=element_blank(), panel.grid.major.x=element_line(colour="white",size=0.1), panel.grid.major.y=element_line(colour="white",size=0.1))


dev.off()

cairo_pdf("Intensity-Barplot-R4_norm.pdf", onefile=TRUE, width=10, height=7)

# Bar Plot / Histogram : Fill colour fixed, with fixed bar width
ggplot(data = RhythmData, aes(x=Time_norm,y=Intensity_norm)) + 
  geom_histogram(stat="identity", width=0.005, position="identity", fill="steelblue") +
  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))

dev.off()