Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions R/RN_dims.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Title
#'
#' @param int_data
#' @param cover_data
#' @param int_type
#'
#' @returns
#' @export
#'
#' @examples
#'
#' # RN_dims_UNI() modified to enter the interaction and cover data
#The size of a network can be described by the number of nodes (*N*) and links (*L*), and its complexity is #proportional to network connectance. *C* can be estimated on different ways depending on the type of #network, but it always measures the proportion of links relative to the maximum number of links that could #be possible in the network. In the case of recruitment networks we use the formula $C = L /(N^2-N)$ since #the node "Open" does not act as a recruit (i.e. Open is represented by a row of zeroes in the adjacency #matrix).
#For facilitation and competition that are bipartite networks, the nodes in eahc guild are #provided( nurse/facilitated, Canopy (depressor)/recruit) seprately, and open is not a node
#'
RN_dims <- function(int_data,cover_data, int_type=c("rec","fac","comp")){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better code structure and robustness, it's recommended to validate the int_type argument using match.arg() at the beginning of the function. Additionally, the series of if statements should be converted to an if/else if chain. This is more efficient as it avoids unnecessary checks once a condition is met, and it more clearly expresses the mutually exclusive nature of the code blocks.

RN_dims <- function(int_data,cover_data, int_type=c("rec","fac","comp")){
  int_type <- match.arg(int_type)


if(int_type=="rec"){

int_data <-comm_to_RN_UNI(int_data,cover_data)

# FUNCTION
df <- int_data
n_nodes <- length(unique(c(df$Canopy, df$Recruit)))
n_links <- sum(df$Pcr)
connectance <- n_links/(n_nodes^2 - n_nodes)

out <- data.frame(c(n_nodes, n_links, connectance))
colnames(out) <- c("Value")
rownames(out) <- c("Num. Nodes", "Num. Links", "Connectance")
return(out)

}

if(int_type=="fac"){

int_data <-suppressWarnings(RN_to_matrix(int_data,cover_data, int_type="fac",weight="Pcr"))

df <-int_data
n_nodes_nurse <-dim(df)[2]
n_nodes_facilitated <-dim(df)[1]
n_links <- sum(df)
connectance <-n_links/(n_nodes_nurse*n_nodes_facilitated)

out <- data.frame(c(n_nodes_nurse,n_nodes_facilitated, n_links, connectance))
colnames(out) <- c("Value")
rownames(out) <- c("Num. Nurse sp","Num. Facilitated sp", "Num. Links", "Connectance")
return(out)

}


if(int_type=="comp"){

int_data <-suppressWarnings(RN_to_matrix(int_data,cover_data, int_type="comp",weight="Pcr"))

df <-int_data
n_nodes_canopy_depressing <-dim(df)[2]
n_nodes_recruit_depressed <-dim(df)[1]
n_links <- sum(df)
connectance <-n_links/(n_nodes_canopy_depressing*n_nodes_recruit_depressed)

out <- data.frame(c(n_nodes_canopy_depressing, n_nodes_recruit_depressed, n_links, connectance))
colnames(out) <- c("Value")
rownames(out) <- c("Num. Canopy depressing sp","Num. Recruit depressed sp", "Num. Links", "Connectance")
return(out)


}


}

174 changes: 174 additions & 0 deletions R/RN_heatmap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#' Title
#'
#' @param int_data
#' @param cover_data
#' @param int_type
#' @param weight
#' @param scale_top
#'
#' @returns
#' @export
#'
#' @examples
#'
#'
RN_heatmap <- function(int_data,cover_data,int_type=c("rec","fac","comp"), weight = c("Pcr","Fcr","Dcr","Dro","Ns", "NintC", "NintA", "RII"), scale_top = 1) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function parameter scale_top is defined but never used within the function body. This constitutes dead code and should be removed to improve clarity.

Additionally, using require(ggplot2) inside a function is not recommended for package development. It's better to list ggplot2 in the Imports field of your DESCRIPTION file and then either use ggplot2:: to call functions or use @importFrom roxygen tags.

RN_heatmap <- function(int_data,cover_data,int_type=c("rec","fac","comp"), weight = c("Pcr","Fcr","Dcr","Dro","Ns", "NintC", "NintA", "RII")) {


if(int_type=="rec"){

index<-suppressWarnings(associndex(int_data,cover_data,expand="yes",rm_sp_no_cover="allsp"))
data<-comm_to_RN_UNI(int_data,cover_data)
data$Dcr<-data$Fcr/data$Ac

# calculate Dro for each recuit species
open_rows <- data[data$Canopy == "Open", ]
ratios <- tapply(open_rows$Fcr / open_rows$Ac, open_rows$Recruit, mean)
data$Dro <- ratios[data$Recruit]


data$int<-paste(data$Canopy,data$Recruit, sep="_")
index$int<-paste(index$Canopy,index$Recruit, sep="_")
db<-merge(data[,c("int","Canopy","Recruit","Fcr","Icr","Pcr","Dcr","Dro")],index[,c("int","Ns", "NintC", "NintA", "RII")], by="int", all.x=T)
db[is.na(db)]<-0

int_data<-db

if (weight %in% c("Ns", "NintC", "NintA", "RII")) {
warning("Since the index specified in the 'weight' argument is defined relative to 'Open', rows or columns labeled 'Open' are mathematically zero and not biologically meaningful")}

require(ggplot2)
# manually set node order
canopy_order <- unique(int_data$Canopy)
canopy_order <- canopy_order[!canopy_order %in% c('Open')]
canopy_order <- c("Open", canopy_order)
int_data$Canopy2 <- factor(int_data$Canopy, levels = canopy_order)
recruit_order <- sort(unique(int_data$Canopy), decreasing = TRUE)
recruit_order <- recruit_order[!recruit_order %in% c('Open')]
recruit_order <- c(recruit_order, "Open")
int_data$Recruit2 <- factor(int_data$Recruit, levels = recruit_order)


# Make weight variable
int_data$weight_v <- as.numeric(int_data[[weight]])

# Lowest (non-zero) and highest values of the weighting variable
highest_W <- max(int_data$weight_v)
lowest_W <- min(int_data$weight_v[int_data$weight_v>0])

# Plot the heatmap
p<-ggplot(int_data, aes(Canopy2, Recruit2, fill= weight_v)) +
geom_tile(aes(height = 1, alpha = weight_v != 0),
colour = "gray", linewidth = 0.25) +
scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0), guide = "none") +
scale_x_discrete(position = "top") +
labs(fill = weight,x = "Canopy", y = "Recruit") +
theme(panel.grid = element_blank(),axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0))

return(p)

}



if(int_type=="fac"){

data<-comm_to_RN_BI(int_data,cover_data)
data$int<-paste(data$Canopy,data$Recruit, sep="_")
df<-suppressWarnings(int_significance_BI (data))
df<-df[df$Effect_int=="Enhancing",]
fac_int<-paste(df$Canopy,df$Recruit, sep="_")
db<-suppressWarnings(associndex_UNISITE_BI(comm_to_RN_BI(int_data,cover_data)))
db$int<-paste(db$Canopy,db$Recruit, sep="_")
db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T)
db<-db[db$int%in%fac_int,]

int_data<-db

require(ggplot2)
# manually set node order
canopy_order <- unique(int_data$Canopy)
int_data$Canopy2 <- factor(int_data$Canopy, levels = canopy_order)
recruit_order <- sort(unique(int_data$Recruit), decreasing = TRUE)
int_data$Recruit2 <- factor(int_data$Recruit, levels = recruit_order)

# Add recruitment density as another weighting variable
int_data$Dcr <- int_data$Fcr/int_data$Ac

# Make weight variable
int_data$weight_v <- as.numeric(int_data[[weight]])

# Lowest (non-zero) and highest values of the weighting variable
highest_W <- max(int_data$weight_v)
lowest_W <- min(int_data$weight_v[int_data$weight_v>0])

# Plot the heatmap
p<-ggplot(int_data, aes(Canopy2, Recruit2, fill= weight_v)) +
geom_tile(aes(height = 1, alpha = weight_v != 0),
colour = "gray", linewidth = 0.25) +
scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0), guide = "none") +
scale_x_discrete(position = "top") +
labs(fill = weight,x = "Canopy", y = "Recruit") +
theme(panel.grid = element_blank(),axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0))

return(p)

}


if(int_type=="comp"){


data<-comm_to_RN_UNI_COMP(int_data,cover_data)
data$int<-paste(data$Canopy,data$Recruit, sep="_")
df<-suppressWarnings(int_significance_BI_COMP (data))
df<-df[df$Effect_int=="Depressing",]
comp_int<-paste(df$Canopy,df$Recruit, sep="_")
db<-suppressWarnings(associndex_UNISITE_BI_COMP(comm_to_RN_UNI_COMP(int_data,cover_data)))
db$int<-paste(db$Canopy,db$Recruit, sep="_")
db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T)
db<-db[db$int%in%comp_int,]

int_data<-db

require(ggplot2)
# manually set node order
canopy_order <- unique(int_data$Canopy)
int_data$Canopy2 <- factor(int_data$Canopy, levels = canopy_order)
recruit_order <- sort(unique(int_data$Recruit), decreasing = TRUE)
int_data$Recruit2 <- factor(int_data$Recruit, levels = recruit_order)

# Add recruitment density as another weighting variable
int_data$Dcr <- int_data$Fcr/int_data$Ac

# Make weight variable
int_data$weight_v <- as.numeric(int_data[[weight]])

if(weight%in% c("Fcr","Icr","Pcr","Dcr","Dro")){
# Lowest (non-zero) and highest values of the weighting variable
highest_W <- max(int_data$weight_v)
lowest_W <- min(int_data$weight_v[int_data$weight_v>0])
}


if(weight %in% c("Ns", "NintC", "NintA", "RII")){
nonzero_vals <- int_data$weight_v[int_data$weight_v != 0]
highest_W <- max(nonzero_vals)
lowest_W <- min(nonzero_vals)
}
# Plot the heatmap
p<-ggplot(int_data, aes(Canopy2, Recruit2, fill= weight_v)) +
geom_tile(aes(height = 1, alpha = weight_v != 0),
colour = "gray", linewidth = 0.25) +
scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0), guide = "none") +
scale_x_discrete(position = "top") +
labs(fill = weight,x = "Canopy", y = "Recruit") +
theme(panel.grid = element_blank(),axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0))

return(p)


}

}


87 changes: 87 additions & 0 deletions R/RN_to_matrix.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#' Title
#'
#' @param int_data
#' @param cover_data
#' @param int_type
#' @param weight
#'
#' @returns
#' @export
#'
#' @examples
#' #This function returns a matrix with canopy species as columns and recruits as rows, containing the association index specified in the weight argument. If int_type is set to "rec," all possible interactions between species with known coverage are displayed. If set to "fac" (or "comp"), a nonzero value appears when recruits associate with canopy plants more (or less) than expected based on their coverage; otherwise, the value is zero.
#'
RN_to_matrix<- function(int_data,cover_data, int_type=c("rec", "fac","comp"), weight = c("Fcr","Dcr","Dro","Ns", "NintC", "NintA", "RII")){

if (!"Open" %in% int_data$Canopy) stop("ERROR: your data does not contain a node named Open or it is spelled differently.")

if(int_type=="rec"){

index<-suppressWarnings(associndex(int_data,cover_data,expand="yes",rm_sp_no_cover="allsp"))
data<-comm_to_RN_UNI(int_data,cover_data)
data$Dcr<-data$Fcr/data$Ac

# calculate Dro for each recruit species
open_rows <- data[data$Canopy == "Open", ]
ratios <- tapply(open_rows$Fcr / open_rows$Ac, open_rows$Recruit, mean)
data$Dro <- ratios[data$Recruit]


data$int<-paste(data$Canopy,data$Recruit, sep="_")
index$int<-paste(index$Canopy,index$Recruit, sep="_")
db<-merge(data[,c("int","Canopy","Recruit","Fcr","Icr","Pcr","Dcr","Dro")],index[,c("int","Ns", "NintC", "NintA", "RII")], by="int", all.x=T)
db[is.na(db)]<-0
net<-suppressWarnings(RN_to_matrix_UNI(db, weight))

if (weight %in% c("Ns", "NintC", "NintA", "RII")) {
warning("Since the index specified in the 'weight' argument is defined relative to 'Open', rows or columns labeled 'Open' are mathematically zero and not biologically meaningful")
}
}

if(int_type=="fac"){

data<-comm_to_RN_BI(int_data,cover_data)
data$int<-paste(data$Canopy,data$Recruit, sep="_")
df<-suppressWarnings(int_significance_BI (data))

if(nrow(df[df$Effect_int=="Enhancing",])==0){

stop("There is not any recruitment enhancing interaction")
}else{

df<-df[df$Effect_int=="Enhancing",]
fac_int<-paste(df$Canopy,df$Recruit, sep="_")
db<-suppressWarnings(associndex_UNISITE_BI(comm_to_RN_BI(int_data,cover_data)))
db$int<-paste(db$Canopy,db$Recruit, sep="_")
db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T)
db<-db[db$int%in%fac_int,]
net<-suppressWarnings(RN_to_matrix_BI(db, weight))
}

}

if(int_type=="comp"){

data<-comm_to_RN_UNI_COMP(int_data,cover_data)
data$int<-paste(data$Canopy,data$Recruit, sep="_")
df<-suppressWarnings(int_significance_BI_COMP (data))

if(nrow(df[df$Effect_int=="Depressing",])==0){

stop("There is not any recruitment depressing interaction")
}else{

df<-df[df$Effect_int=="Depressing",]
comp_int<-paste(df$Canopy,df$Recruit, sep="_")
db<-suppressWarnings(associndex_UNISITE_BI_COMP(comm_to_RN_UNI_COMP(int_data,cover_data)))
db$int<-paste(db$Canopy,db$Recruit, sep="_")
db<-merge(db, data[,c("int","Icr","Pcr")], by="int", all.x=T)
db<-db[db$int%in%comp_int,]
net<-suppressWarnings(RN_to_matrix_BI(db, weight))
}
}


return(net)
}

Loading
Loading