knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

In this example, we will report the daily key presses that a bird has made. The data will be analyzed only until one of the songs was exhausted each day.

Importing data

First, we will import our data. To do so, we need only the name of the directory containing the files to be imported.

songPreference already comes with sample data from one bird, named ZF1536. We will import the data from this bird. For that, we need to know where the installation directory of songPreference resides.

songPreference_dir <- find.package(package = "songPreference")
data_dir <- file.path(songPreference_dir, "ZF1536")
print(data_dir)

Next, we import the data using the command loadOC from songPreference. To use this command, we need to load songPreference.

library(songPreference)
data <- loadOC(birDir = data_dir)
head(data)

Estimate age

We can easily estimate age if we know the date when the bird was hatched. We need to provide that date.

hatching_date <- "2015-12-18"

Then, we just need to execute the following code.

hatching_date <- as.POSIXct(hatching_date, format= "%Y-%m-%d")
data$age <- data$dates - as.Date(hatching_date)

head(data)

Filtering data

We will now filter the data to keep only records until one of the songs was exhausted, each day. This is done by using the function filterByLastPb.

data_filtered <- filterByLastPb(data = data, filtype = "onesong")

Getting the press count

A summary of daily presses for each key can be obtained using the function getKeyCount.

daily_summary <- getKeyCount(data = data_filtered)
head(daily_summary)

The date is in numerical format. See ?Date for more information.

Plotting the proportion of daily presses

We will now plot the above data. We will build a plot of stacked areas representing the proportion of presses for each key along time.

First, we need to ccalculate the proportion of presses for key A.

proportion_songA <- apply(X = daily_summary, MARGIN= 1, FUN = function(x) {
  x[1]/(x[1] + x[2])
})
head(proportion_songA)

Now, if the names of the vector are dates, we need to convert the dates in numerical format to a more legible format.

dates_are_names <- is.element("dates", names(attr(daily_summary, "dimnames")))
if (dates_are_names){
  names(proportion_songA) <- as.Date(as.numeric(names(proportion_songA)), 
                               origin= "1970-01-01")
}

head(proportion_songA)

To facilitate the construction of the plots, we will arrange the data into a dataframe.

if (dates_are_names){
  proportions <- data.frame(date= as.Date(names(proportion_songA)))
}else{
  proportions <- data.frame(age= as.numeric(names(proportion_songA)))
}

proportions$songA <- proportion_songA
proportions$songB <- 1 - proportion_songA

colnames(proportions)[2] <- colnames(daily_summary)[1]
colnames(proportions)[3] <- colnames(daily_summary)[2]
head(proportions)

The dataframe is in wide format. It must be converted to long format before plotting.

library(reshape2)
id <- ifelse(test = dates_are_names, yes = "date", no = "age")
proportions_long <- melt(data = proportions, id= id, value.name = "proportion",
                         variable.name = "key_label")
head(proportions_long)

Now we can make the plot.

library(ggplot2)
p <- ggplot(data = proportions_long, mapping = aes(x = eval(parse(text= id)), 
                                                   y = proportion, 
                                                   fill= key_label))
p <- p + geom_area() + xlab(id)
print(p)

Exporting the data

Data can be exported through the use of a basic function such as write.csv. Check the help file for that function for more information. You can export either data, daily_summary, or proportions_long in this way.



crodriguez-saltos/songPreference documentation built on Sept. 16, 2019, 7:09 a.m.