First we need to create an example data, which would contain following information:
Unique ID Column: The first column will contain unique identifiers for each row.
Having a distinct row name is recommended as it can help detect potential data errors.
Treatment Column: The second column represents a hypothetical factor with three levels of impact: “high,” “medium,” and “low.”
Species Abundance Columns: Columns 3 to 7 correspond to hypothetical species and their abundance in different sites (rows).
You can take a look at data by clicking the tab Example Data.
df <- data.frame(ID = c("ID01", "ID02", "ID03", "ID04", "ID05", "ID06", "ID07", "ID08", "ID09", "ID10", "ID11", "ID12", "ID13", "ID14", "ID15"),
Treatment = c("High", "Low", "Medium", "High", "Low", "Medium", "High", "Low", "Medium", "High", "Medium", "High", "Low", "Low", "High"),
Species1 = rpois(15, 10),
Species2 = rpois(15, 5),
Species3 = rpois(15, 8),
Species4 = rpois(15, 12),
Species5 = rpois(15, 6))
Although we already have an example data, we should transform it to the long format data for convenience.
For that we can use melt function from the reshape2 package.
df_long <- df %>%
melt(id = c("ID", "Treatment")) %>%
rename(Species = variable, Value = value)
Data in long format will be look like this:
## ID Treatment Species Value
## 1 ID01 High Species1 10
## 2 ID02 Low Species1 7
## 3 ID03 Medium Species1 5
## 4 ID04 High Species1 9
## 5 ID05 Low Species1 11
## 6 ID06 Medium Species1 10
Now let’s calculate alpha diversity metrics using vegan.
This is how it will looks like on a Treatment type level.
df_long %>%
group_by(Treatment) %>%
summarise(Sprichness = specnumber(Value),
Shannon = diversity(Value, index = "shannon"),
Simpson = diversity(Value, index = "simpson"),
InvSimpson = 1/Simpson,
Abundance = sum(Value))
## # A tibble: 3 × 6
## Treatment Sprichness Shannon Simpson InvSimpson Abundance
## <chr> <int> <dbl> <dbl> <dbl> <int>
## 1 High 30 3.32 0.961 1.04 246
## 2 Low 25 3.11 0.951 1.05 219
## 3 Medium 20 2.85 0.936 1.07 164
This is how it will look if we want to chech alpha diversity on a Site level.
df_long %>%
group_by(ID) %>%
summarise(Sprichness = specnumber(Value),
Shannon = diversity(Value, index = "shannon"),
Simpson = diversity(Value, index = "simpson"),
InvSimpson = 1/Simpson,
Abundance = sum(Value))
## # A tibble: 15 × 6
## ID Sprichness Shannon Simpson InvSimpson Abundance
## <chr> <int> <dbl> <dbl> <dbl> <int>
## 1 ID01 5 1.60 0.796 1.26 40
## 2 ID02 5 1.43 0.740 1.35 38
## 3 ID03 5 1.46 0.740 1.35 39
## 4 ID04 5 1.56 0.784 1.28 40
## 5 ID05 5 1.51 0.761 1.31 47
## 6 ID06 5 1.47 0.753 1.33 44
## 7 ID07 5 1.53 0.771 1.30 30
## 8 ID08 5 1.56 0.781 1.28 49
## 9 ID09 5 1.39 0.724 1.38 44
## 10 ID10 5 1.54 0.769 1.30 53
## 11 ID11 5 1.55 0.774 1.29 37
## 12 ID12 5 1.48 0.752 1.33 47
## 13 ID13 5 1.46 0.738 1.35 49
## 14 ID14 5 1.58 0.789 1.27 36
## 15 ID15 5 1.60 0.795 1.26 36