Coder Social home page Coder Social logo

odonata_traits's Introduction

British Odonata trait data

This repository contains the odonata trait data that are held within the Biological Records Centre (BRC). The scripts in this repository can be used to produce an overview of the odonata trait data measurements, [odonata_trait_summary.Rmd] (https://github.com/BiologicalRecordsCentre/Odonata_traits/blob/master/Scripts/Odonata_trait_summary.Rmd). Additionally, we have uploaded a script that combines the trait measurement data to the single value per trait data set, [Combine_odonata_trait_data.r] (https://github.com/BiologicalRecordsCentre/Odonata_traits/blob/master/Scripts/Combine_odonata_trait_data.r).

Overview of the trait measurements

This script, [odonata_trait_summary.Rmd] (https://github.com/BiologicalRecordsCentre/Odonata_traits/blob/master/Scripts/Odonata_trait_summary.Rmd), acts a simple tutorial for gaining a quick overview of the odonata trait measurements held within the Biological Records Centre (BRC).

Trait summaries

First, we need to ensure R is clear, add the trait data and install and load the lattice and reshape2 packages:

rm(list=ls()) # clear R
Odo_data <- read.csv(paste0(getwd(),"/Data/Species_trait_measurements.csv"),header=T) 
# install.packages("lattice")
# install.packages("reshape2")
library("lattice")
library("reshape2")

Next, we want to create a series of box plots that summarises the trait measurement data. We may want to produce species specific trait summaries or a summary across all species. We have written a simple function where the user specifies name of the species they want a trait summary for:

trait_summary(x="Anax imperator")

or alternatively the user can specify all species:

trait_summary()

A list of the 37 species that we have trait data for can be extracted from the species column:

levels(Odo_data$Species)

We use the bwplot from lattice to produced the panelled box plots. The first step in the function drops unused columns. Next, the melt function is used to reshape the data to fit the format required by lattice. We specify the factors that will be used to produce the box plots as trait.f and Sex.f. trait.f is the trait factor, we use labels to give sensible names to the traits. Similarly we give sensible labels to the sex trait, Sex.f:

trait_summary <- function(x="all_species") {
  if(x=="all_species"){
    temp_data <- Odo_data
  } else {
    temp_data <- Odo_data[Odo_data$Species==x,]
  }
  
  # prepare data
  temp_data <- temp_data[,c(6:length(temp_data))]
  Odo <- melt(temp_data, id=c("Species", "Sex"), na.rm=TRUE)
  
  # create factors with value labels
  trait.f <- factor(Odo$variable,levels=names(temp_data)[3:13],
    labels=c("Thorax length","Thorax width","Thorax depth","L forewing length","L forewing width",
              "L hindwing length","L hindwing width","R forewing length","R forewing width",
              "R hindwing length","R hindwing width"))
  Sex.f <- factor(Odo$Sex,levels=c("f","m"),
    labels=c("Female","Male"))

  # identify y axis limits
  T_max <- ceiling(max(Odo[Odo$variable=="thorax_length"|Odo$variable=="thorax_width"|Odo$variable=="thorax_depth","value"]))
  T_min <- floor(min(Odo[Odo$variable=="thorax_length"|Odo$variable=="thorax_width"|Odo$variable=="thorax_depth","value"]))
  WL_max <- ceiling(max(Odo[Odo$variable=="left_forewing_length"|Odo$variable=="left_hindwing_length"|
                  Odo$variable=="right_forewing_length"|Odo$variable=="right_forewing_length","value"]))
  WL_min <- floor(min(Odo[Odo$variable=="left_forewing_length"|Odo$variable=="left_hindwing_length"|
                  Odo$variable=="right_forewing_length"|Odo$variable=="right_forewing_length","value"]))
  WD_max <- ceiling(max(Odo[Odo$variable=="left_forewing_width"|Odo$variable=="left_hindwing_width"|
                  Odo$variable=="right_forewing_width"|Odo$variable=="right_forewing_width","value"]))
  WD_min <- floor(min(Odo[Odo$variable=="left_forewing_width"|Odo$variable=="left_hindwing_width"|
                  Odo$variable=="right_forewing_width"|Odo$variable=="right_forewing_width","value"]))
  
  
  # produce the panelled box plots
  bwplot(Odo$value~Sex.f|trait.f,
    ylab=list("Value (mm)",cex=1.3), 
    xlab=list("Sex",cex=1.3),
    ylim=list(c(T_min,T_max),c(T_min,T_max),c(T_min,T_max),
              c(WL_min,WL_max),c(WD_min,WD_max),c(WL_min,WL_max),c(WD_min,WD_max),
              c(WL_min,WL_max),c(WD_min,WD_max),c(WL_min,WL_max),c(WD_min,WD_max)),
    main=list("Trait summary",cex=1.5),
    par.strip.text=list(cex=0.9),
    scales = list(y = list(relation = "free")),
    index.cond=list(c(4,8,6,10,5,9,7,11,1,2,3)),
    layout=(c(4,3))
    )  
}

Examples

Trait summary across all species

trait_summary()

Trait summary for Anax imperator

trait_summary(x="Anax imperator")

odonata_traits's People

Contributors

gpowney avatar

Watchers

Katja Schulz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.