Reputation: 11
I have two datasets, and I need to create a dataframe that allows me to select one column for each data set, and create a boxplot. For instance, one dataset has information about Treatment, and the other dataset has a Shannon diversity index measurement
library(ggplot2)
library(vegan)
library(tidyverse)
# This code will create a matrix with 30 columns representing species and 10 rows representing communities.
# The object will be called "community_matrix"
set.seed(2)
community1=matrix(
sample(0:250,300,replace=T),nrow=10,
dimnames=list(paste("community",1:10,sep=""),paste("sp",1:30,sep="")))
community2=matrix(
sample(50:200,300,replace=T),nrow=10,
dimnames=list(paste("community",11:20,sep=""),paste("sp",1:30,sep="")))
community_matrix<- rbind(community1,community2)
#This code will create a metadata table depicting the treatments.
m1<- as.data.frame(list(paste("community",1:10,sep=""), paste("T1")))
colnames(m1)<- c("Communities", "Treatment")
m2<- as.data.frame(list(paste("community",11:20,sep=""), paste("T2")))
colnames(m2)<- c("Communities", "Treatment")
metadata<-rbind(m1,m2)
# 1- Calculate the Shannon diversity index for all samples using the vegan package
raremax<-min(rowSums(community_matrix))
rarefied_data<- rrarefy(community_matrix, raremax)
H <- vegan::diversity(rarefied_data, index = "shannon")
# Using the Shannon diversity measurements obtained in step1, make a graph with a boxplot for each treatment - customize the plot as you wish```
Upvotes: 0
Views: 71
Reputation: 206232
One way to do this is to join/merge the data. The only trick here is that H is a named vector rather than a data frame. But if we use a bit of dplyr
to fo the merge, we can also use tibble
to turn it into a data frame. This should work
tibble::enframe(H, name="Communities", value="H") |>
left_join(metadata) |>
ggplot() +
aes(Treatment, H) +
geom_boxplot()
Upvotes: 2