linkET R Package

repository·master·Indexed 42 days ago

https://github.com/hy4m/linket

An R package for visualizing matrix heatmaps, correlation matrices, and Mantel tests using the ggplot2 framework. It provides tools for calculating correlation coefficients via correlate(), creating heatmaps with hyplot() and qcorrplot(), and visualizing Mantel test results using geom_couple().

Tokens
1.3K
Snippets
9
Records
9
Agent score
3%

What's inside linkET

  1. Install linkET from GitHub

    master

    You can install the released version of linkET using devtools from GitHub.

    # install.packages("devtools")
    devtools::install_github("Hy4m/linkET", force = TRUE)
    packageVersion("linkET")
  2. Perform Mantel tests and visualize with geom_couple()

    master

    The mantel_test() function performs Mantel tests between two datasets (e.g., species and environment). The results can be visualized alongside a correlation plot using geom_couple(), which draws links between variables. Use nice_curvature() to adjust the link paths.

    library(dplyr)
    library(vegan)
    
    # Perform Mantel test
    mantel <- mantel_test(varespec, varechem, 
                          spec_select = list(Spec01 = 1:7, Spec02 = 8:18)) %>% 
      mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf), labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
             pd = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf), labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))
    
    # Visualize correlation matrix with Mantel links
    qcorrplot(correlate(varechem), type = "lower", diag = FALSE) +
      geom_square() +
      geom_couple(aes(colour = pd, size = rd), 
                  data = mantel, 
                  curvature = nice_curvature())
  3. Create heatmaps with hyplot()

    master

    Use hyplot() to create heatmaps from matrix_data objects. It integrates with ggplot2, allowing you to add standard layers like geom_tile() or geom_point().

    library(linkET)
    library(ggplot2)
    
    ## Heatmap with geom_tile
    matrix_data(list(mtcars = mtcars)) %>% 
      hyplot(aes(fill = mtcars)) +
      geom_tile()
    
    ## Point plot with geom_point
    as_md_tbl(mtcars) %>% 
      hyplot(aes(size = mtcars)) +
      geom_point(shape = 21, fill = NA)
  4. Configure and reset correlation plot styles

    master

    You can modify the global appearance of correlation plots using set_corrplot_style() and revert to the original settings using set_default_style().

    ## Set custom style
    set_corrplot_style()
    qcorrplot(mtcars) + geom_square()
    
    ## Reset to default
    set_default_style()
  5. Convert data to matrix_data objects

    master

    The matrix_data() function converts a list of data frames into a specialized matrix_data object used for visualization. Alternatively, you can use as_matrix_data() to convert a single data frame directly.

    library(linkET)
    ## Using matrix_data with a list
    matrix_data(list(mtcars = mtcars))
    
    ## Using as_matrix_data with a single data frame
    as_matrix_data(mtcars)
  6. Convert matrix_data to markdown table format

    master

    To transform a matrix_data object or a standard data frame into a long-format tibble (markdown table style), use as_md_tbl().

    ## From matrix_data
    matrix_data(list(mtcars = mtcars)) %>% 
      as_md_tbl()
    
    ## From a data frame
    as_md_tbl(mtcars)
  7. Calculate correlation matrices with correlate()

    master

    The correlate() function computes correlation coefficients and p-values. The output can be converted into matrix_data or a markdown table for plotting.

    ## Convert correlation results to matrix_data
    correlate(mtcars) %>% 
      as_matrix_data()
    
    ## Convert correlation results to markdown table
    correlate(mtcars) %>% 
      as_md_tbl()
  8. Create correlation plots with qcorrplot()

    master

    The qcorrplot() function is designed specifically for correlation matrices. You can specify the type (e.g., "lower") and use geom_square() to render the plot. If the input is not a correlation matrix, you can override this behavior using the is_corr parameter.

    ## Basic correlation plot
    correlate(mtcars) %>% 
      as_md_tbl() %>% 
      qcorrplot() +
      geom_square()
    
    ## Lower triangle correlation plot
    qcorrplot(varespec[1:30], type = "lower") +
      geom_square()