ggseg / ggseg3d

ggseg3d R package for cisualising brain atlases through plotly
https://ggseg.github.io/ggseg3d/
Other
27 stars 9 forks source link

Customizing for opacity of the subcortical structure #21

Open zhangjb35 opened 6 days ago

zhangjb35 commented 6 days ago

Seem the opacity of subcortical structure cannot control by the opacity parameter of ggseg3d.

library(ggsegDefaultExtra)
library(ggseg3d)
library(dplyr)
library(tidyr)
library(plotly)

# setup region to display
labs <- c("Left-Hippocampus.ant","Left-Hippocampus.post")
aseg_filt <- hcpa_3d |> 
  unnest(ggseg_3d) |> 
  filter(
    # Of you 'dont want the ones in the vector
    label %in% labs
  ) |> 
  as_ggseg3d_atlas()

# setup color for ant and post region
somData_aseg = hcpa_3d %>% 
  unnest(col = ggseg_3d) %>% 
  filter(grepl("Left-Hippocampus.ant|Left-Hippocampus.post", label)) %>% 
  select(label) %>% 
  mutate(myCol = case_when(
    grepl("Left-Hippocampus.ant", label) ~ "#BD1E2D",
    grepl("Left-Hippocampus.post", label) ~ "#fbb041",
  ))

p <- ggseg3d(.data = somData_aseg, atlas = aseg_filt, na.alpha=0.1, show.legend = F, colour = "myCol", alphahull=0,opacity=0.1) %>% 
  add_glassbrain("left") %>%
  pan_camera("left lateral") %>%
  add_trace(x = 50, y = 30, z = 40, type = "scatter3d", mode = "markers", marker = list(color = 'rgb(0,0,0)',size=2), inherit = T, showlegend = F) %>%
  add_trace(x = 50, y = 20, z = 20, type = "scatter3d", mode = "markers", marker = list(color = 'rgb(0,0,0)',size=2), inherit = T, showlegend = F) %>%
  add_trace(x = 40, y = 10, z = 40, type = "scatter3d", mode = "markers", marker = list(color = 'rgb(0,0,0)',size=2), inherit = T, showlegend = F) %>%
  add_trace(x = 30, y = -2, z = 40, type = "scatter3d", mode = "markers", marker = list(color = 'rgb(0,0,0)',size=2), inherit = T, showlegend = F) %>%
  add_trace(x = 20, y = -12, z = 40, type = "scatter3d", mode = "markers", marker = list(color = 'rgb(0,0,0)',size=2), inherit = T, showlegend = F) %>%
  add_trace(x = -21, y = -17, z = -23, type = "scatter3d", mode = "markers", marker = list(color = 'rgb(0,148,68)',size=8), inherit = T, showlegend = F) %>%
  remove_axes()
p
zhangjb35 commented 6 days ago

Seem just change the op setup in ggseg3d can achieve it.

p = plotly::plot_ly()
    for (tt in 1:nrow(atlas3d)) {
        col = rep(unlist(atlas3d[tt, fill]), nrow(atlas3d$mesh[[tt]]$faces))
        col = ifelse(is.na(col), na.colour, col)
        op = ifelse(is.na(unlist(atlas3d[tt, fill])), na.alpha, 
            0.1)
        txt = if (is.null(text)) {
            text
        }
        else {
            paste0(text, ": ", unlist(atlas3d[tt, text]))
        }
        p = plotly::add_trace(p, x = atlas3d$mesh[[tt]]$vertices$x, 
            y = atlas3d$mesh[[tt]]$vertices$y, z = atlas3d$mesh[[tt]]$vertices$z, 
            i = atlas3d$mesh[[tt]]$faces$i - 1, j = atlas3d$mesh[[tt]]$faces$j - 
                1, k = atlas3d$mesh[[tt]]$faces$k - 1, facecolor = col, 
            type = "mesh3d", text = txt, showscale = FALSE, opacity = op, 
            name = unlist(atlas3d[tt, label]), ...)
    }