ggseg

repository·main·Indexed 42 days ago

https://github.com/ggsegverse/ggseg

An R package for visualizing neuroimaging results, such as cortical thickness or p-values, on brain figures. It integrates with ggplot2 by treating brain atlas geometries as simple features, providing built-in atlases like dk(), aseg(), and tracula(), and offering a geom_brain() function for mapping data to brain figures.

Tokens
674
Snippets
4
Records
4
Agent score
5%

What's inside ggseg

  1. Install ggseg

    main

    You can install the stable version of ggseg from CRAN or the development version from the ggsegverse r-universe.

    Install from CRAN

    install.packages("ggseg")

    Install development version from ggsegverse r-universe

    To access development versions, set the repository options before installing:

    options(repos = c(
      ggsegverse = "https://ggsegverse.r-universe.dev",
      CRAN = "https://cloud.r-project.org"
    ))
    install.packages("ggseg")
  2. Explore built-in brain atlases

    main

    The ggseg package includes three built-in atlases:

    • dk(): Desikan-Killiany cortical parcellation.
    • aseg(): Automatic subcortical segmentation.
    • tracula(): White matter tracts.

    You can quickly visualize these atlases using the plot() function.

    library(ggseg)
    library(ggplot2)
    
    plot(dk())
    plot(aseg())
  3. Install additional atlases from ggsegverse

    main

    Beyond the built-in atlases, you can install specialized atlases from the ggsegverse r-universe. For example, to install the Yeo 2011 atlas:

    install.packages("ggsegYeo2011", repos = "https://ggsegverse.r-universe.dev")
  4. Plot neuroimaging data with geom_brain()

    main

    To map neuroimaging results (like p-values or cortical thickness) onto a brain figure, use geom_brain() within a ggplot2 call.

    Requirements:

    1. Your data frame must contain a column that matches the atlas labels (typically named region or label).
    2. Use atlas = <atlas_function>() to specify the geometry.
    3. Use position = position_brain(hemi ~ view) to define the hemisphere and view orientation.

    Example mapping a p value to the fill aesthetic:

    library(dplyr)
    library(ggseg)
    library(ggplot2)
    
    some_data <- tibble(
      region = rep(
        c(
          "transversetemporal",
          "insula",
          "precentral",
          "superiorparietal"
        ),
        2
      ),
      p = sample(seq(0, .5, .001), 8),
      groups = c(rep("g1", 4), rep("g2", 4))
    )
    
    ggplot(some_data) +
      geom_brain(
        atlas = dk(),
        position = position_brain(hemi ~ view),
        aes(fill = p)
      ) +
      facet_wrap(~groups) +
      scale_fill_viridis_c(option = "cividis", direction = -1) +
      theme_void()