knitr::opts_chunk$set(echo = TRUE)

Introduction

Clean and tidy data taken from leaf litter samples.

Preliminaries

Load libraries

library(here)
library(reshape2)
library(stringr)
library(vegan)
library(mvabund)
library(ggplot2)
library(cowplot)
library(lme4)
library(blme)
library(coefplot)
library("rptR")

Load data

Construct file path

file. <- here::here("data-raw",
           "data-raw-insects",
           "data-raw-leaf-litter",
           "CSV_leaf_litter_both_sites",
           "leaf_litter_insects_CSV.csv")

load data

litter <- read.csv(file= file.,
                   skip = 6,
                   header = TRUE,
                   blank.lines.skip = TRUE,
                   na.strings = "")

Check wide data

explore <- TRUE
if(explore  == TRUE){
  head(litter)

  summary(litter,15)

  dim(litter)
}

Clean wide data

Remove blank rows

litter <- na.omit(litter)

Change column names

names(litter) <- gsub("^X","S",names(litter))

Wide data structure

head(litter)
   site month      taxa S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 S13 S14 S15 S16 S17 S18

1 Broadleaf Jan Spiders 0 0 0 2 3 2 1 1 0 1 2 1 6 1 0 2 0 0 2 Broadleaf Jan Ants 0 0 0 4 1 1 3 0 0 0 1 0 5 0 0 0 0 0 3 Broadleaf Jan Cockroach 0 1 0 0 1 2 1 0 0 2 1 0 5 1 0 0 0 1 4 Broadleaf Jan Beetle 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 5 Broadleaf Jan Cricket 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 Broadleaf Jan Gusano 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0

Melt data

Melt

Melt data from wide to long.

id.vars. <- names(litter)[-grep("^S",names(litter))]
litter.melt <- melt(data = litter,
                    id.vars = id.vars.,
                    variable.name = "sample",
                    value.name = "N")

Clean melted data

summary(factor(litter.melt$sample))
litter.melt$sample <- gsub("S","",litter.melt$sample )
summary(factor(litter.melt$sample))
litter.melt$sample <- as.numeric(litter.melt$sample)

Check cleaned wide data

explore <- TRUE
wrking <- litter.melt
if(explore  == TRUE){
  head(wrking)

  summary(wrking,15)

  dim(wrking)
}

Plot

litter.melt$site <- factor(litter.melt$site,
                           levels = c("La Cueva","La Caoba","Morelia","El Corral",
                                      "Broadleaf"))
ggplot(data = litter.melt,
       aes(y = N,
           x = site,
           color = site)) +
  geom_boxplot() +
facet_wrap(~ taxa,scale = "free")

ggplot

 #ggplot2 time saver: calculate mean SE/CIs on the fly
ggplot(data = litter.melt,
       aes(y = N,
           x = site,
           color = site)) +
  stat_summary(fun.data  = "mean_cl_boot",size = 2)
library(ggpubr)

ggerrorplot(data = litter.melt,
            y = "N",
            x = "site",
            color = "site",
            desc_stat = "mean_ci",
            size = 2)

Save Long data

litter <- litter.melt
save(litter, file = "./data/litter.RData")


brouwern/DRmencia documentation built on May 6, 2019, 12:24 p.m.