Chapter 6 musicnotationR


musicnotationR is an R package for producing music notation social graphs. Currently there is one main function, musicnot() which can be used to plot temporally organized social interaction data according to the musical notation visualization method of Ivan Chase (2006) “Music notation: a new method for visualizing social interaction in animals and humans”, Frontiers Zoology 3: 18.


6.1 Installation

The package can be installed directly from GitHub using the install_github function from the devtools library.


library(devtools)
install_github('jalapic/musicnotationR', username = "jalapic")
library(musicnotationR)

 

6.1.1 Sample Data Sets

There are currently two example datasets - ‘dom’ and ‘flies’. The format of these dataframes are as follows:

head(dom)
##   Time Winner Loser behavior
## 1    1      A     H     peck
## 2    2      A     I     peck
## 3    3      A     I     peck
## 4    4      A     D     peck
## 5    5      A     H     peck
## 6    6      A     E     peck
head(flies)
##   Time     fly1    fly2 behavior
## 1 0.00   George Raymond     push
## 2 0.14     Ivan  George     push
## 3 0.30   George    Ivan     push
## 4 0.45  Raymond    Ivan     poke
## 5 0.54   George    Ivan    tease
## 6 0.63 Vladimir Raymond     poke

 

As can be seen, each dataframe consists of four variables. These are in order:

  • col1: a numeric unit of time at which each behavior occured
  • col2: the individual performing/directing the behavior
  • col3: the individual receiving the behavior
  • col4: the behavior

 

Important notes: The first three columns have to be included in that order, however, they can be named anything. If behavior is included it has to be in the fourth column, but again can be named anything. As many other variables/columns as desired can be included in the dataframe and they won’t interfere with graphing.

 

For instance, the following randomly generated data are also in an acceptable format:

  set.seed(84)
  datetime <- random_datetime(100, st="2015/01/01", et="2015/01/31")
  indivs <- matrix(replicate(100, sample(LETTERS[1:6], 2)), ncol=2, byrow=T)

mydf <- data.frame(datetime, indivs)
colnames(mydf) <- c("datetime", "indiv1", "indiv2")
head(mydf)
##              datetime indiv1 indiv2
## 1 2015-01-01 04:06:48      F      D
## 2 2015-01-01 08:31:25      C      D
## 3 2015-01-01 15:03:32      C      D
## 4 2015-01-02 22:29:50      B      A
## 5 2015-01-03 00:15:26      C      F
## 6 2015-01-03 06:05:46      E      C

 

The datetime variable above is generated randomly between a start and end date using the random_datetime function built in to the musicnotationR package.

 

6.1.2 Plotting Music Notation Social Graphs

Music notation social interaction graphs can be plotted using the musicnot() function, which contains many optional arguments for customizing the plot. This function utilizes options built into the ggplot2 and grid graphing packages, but by compiling them into one functions, makes manipulation easier.

The basic plot of any data set is to use all the default arguments. For example:

musicnot(flies)

 

This plots on the x-axis the time (or date) at which each behavioral event occurred. Each y-axis gridline represents an individual. The default is to have gray gridlines. Individuals are reprented by numbers (in this example 1-4 as there are four individuals). In the default state, the number of each individual is determined by sorting alphanumerically. The color scheme used for plotting is the default ggplot2 color scheme.

In the next series of plots, I will showcase several of the optional arguments.

To add color to the horizontal gridlines:

 

musicnot(flies, gridcolor=T)

 

To change the weight of the gridlines:

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5)

 

To change the y-axis tick labels to names/characters rather than numbers:

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name")

 

To add a y-axis title:

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", ylabel="Individual")

 

To change the size of the data point:

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", ylabel="Individual", pointsize=2)

 

There are also a plethora of options for chaging the colors of individuals. For instance…

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", colors="topo")

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", colors="rainbow")

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", 
         colors=c("firebrick1", "dodgerblue", "seagreen", "darkgoldenrod1"))

 

If the fourth column of the dataframe contains information about which behavior occurred at each time point, then it is possible to plot these different behaviors by adjusting the linetype with the behav="yes" argument. The linetypes will be selected based on the order in which each behavior occurs initially in the dataframe, but can be manually adjusted also. For example:

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", 
         colors=c("firebrick1", "dodgerblue", "seagreen", "darkgoldenrod1"), 
         behav="yes")

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", 
         colors=c("firebrick1", "dodgerblue", "seagreen", "darkgoldenrod1"), 
         behav=c("tease", "push", "poke"))

 

It’s also possible to manually change the order in which individuals are plotted on the y-axis. The input should be a character vector containing the names of all individuals.

 

musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", 
         colors=c("firebrick1", "dodgerblue", "seagreen", "darkgoldenrod1"),     
         ranks=c("Vladimir", "Raymond", "George", "Ivan"))

 

Another example using the above randomly generated ‘mydf’ dataset:

 

musicnot(mydf, gridcolor=T, gridlinesize = 0.25, labels="name", 
         pointsize=1.5, ranks = c("E", "B", "A", "D", "F", "C"), colors="rainbow")

 

6.1.3 Further customization of music notation plots

 

Because the musicnot() function is essentially a wrapper for ggplot2 code, each plot can be further altered by adding ggplot2 code. For instance,

p <- musicnot(flies, gridcolor=T, gridlinesize = 0.5, labels="name", 
              colors=c("black", "orange1", "limegreen", "dodgerblue1"))

p + 
  ggtitle("Social Interactions of Flies") +
  xlab("Time in seconds") +
  theme(panel.background = element_rect(fill = "mistyrose1"),
        panel.grid.minor = element_blank() 
  )