Skip to content

ggtree conflict between viewClade,scale_y_reverse, rotate #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yoelii opened this issue May 6, 2025 · 0 comments
Open

ggtree conflict between viewClade,scale_y_reverse, rotate #652

yoelii opened this issue May 6, 2025 · 0 comments

Comments

@yoelii
Copy link

yoelii commented May 6, 2025

Hi,

I have a ggtree tree object and I want to explore them in a RShiny app. My ggtree is not compatible with plotly plot (I losted highlight, clade, ..).
To conturn incompability with plotly, I use ggtree in a simple plot. With help of Shiny, I made a kind a interactivity with some selectInput Menu and a kind of zoom with viewClade() function.

But with viewClade, I losted the order of my branches. I need to rotate it. But with rotatation, the highlight become incorrect.

Please find following my code with a more simple tree but reproducible example of my problem.

Thanks for your help

# Load libraries
library(shiny)
library(ggtree)
library(ggplot2)
library(ape)
library(TDbook)
#install.packages("gridExtra")
library(gridExtra)

# Define colors for tree
trophic_colors <- c("carnivore" = "red", "herbivore" = "green", "omnivore" = "blue")
trophic_shapes <- c("carnivore" = 15, "herbivore" = 16, "omnivore" = 17)

# Prepare data for selectInput
p <- ggtree(tree_boots) %<+% df_tip_data
node_data <- p$data
internal_nodes <- node_data[node_data$isTip == FALSE, c("label","node")]
nodes_for_select <- internal_nodes$node
names(nodes_for_select) <- internal_nodes$label

nodes_for_select <- as.list(nodes_for_select)
label_nodes_h <- internal_nodes$label
names(label_nodes_h) <- internal_nodes$node

nodes_for_select_zoom <- node_data[node_data$isTip == TRUE, c("label","node")]
list_nodes_for_select_zoom <- nodes_for_select_zoom$node
names(list_nodes_for_select_zoom ) <- nodes_for_select_zoom$label
list_nodes_for_select_zoom <- as.list(list_nodes_for_select_zoom)

# define ui
ui <- fluidPage(
  titlePanel("Tree with ggtree"),
  sidebarLayout(
    sidebarPanel(
      selectInput("nodeSelect", "Select a node of interest to highlight :",
                  choices = list_nodes_for_select_zoom,
                  selected = list_nodes_for_select_zoom$Rangifer_tarandus),

                  # selected = nodes_for_select$Cervidae),
      checkboxGroupInput("trophicHabitSelect", " Choose trophic habits to display :",
                         choices = unique(df_tip_data$trophic_habit),
                         selected = unique(df_tip_data$trophic_habit)),
      selectInput("zoomNode", "Select a node to zoom:",
                  choices = nodes_for_select,
                  selected = nodes_for_select$Mammalia)
    ),
    mainPanel(
      plotOutput("treePlot"), 
      plotOutput("zoomPlot")
    )
  )
)

# Defin server part
server <- function(input, output) {
  output$treePlot <- renderPlot({
    # Filter data according to user's selection
    selected_habits <- input$trophicHabitSelect
    df_filtered <- df_tip_data[df_tip_data$trophic_habit %in% selected_habits, ]
    
    selected_colors <- trophic_colors[selected_habits]
    selected_shapes <- trophic_shapes[selected_habits]
    
    df_filtered$trophic_habit <- factor(df_filtered$trophic_habit, levels = selected_habits)
    
    # Made the complete Tree with ggtree 
    p_full <- ggtree(tree_boots) %<+% df_filtered + xlim(-0.1, 4)
    p_full <- p_full + geom_tiplab(offset = 0.6, hjust = 0.5) +
      geom_point(aes(shape = trophic_habit, color = trophic_habit,
                     size = mass_in_kg), show.legend = TRUE, na.rm = TRUE) +
      theme(legend.position = "right") +
      scale_size_continuous(range = c(3, 10)) +
      scale_shape_manual(values = selected_shapes) +
      scale_color_manual(values = selected_colors)+ scale_y_reverse()
    
    # Add highlight
    selected_node <- input$nodeSelect
    p_full <- p_full + geom_highlight(node = as.numeric(selected_node),
                                      fill = "NA", alpha = 0.01, extend = 1,
                                      color = "deepskyblue", linetype = 3)
 
    p_full
    
  })
  
  output$zoomPlot <- renderPlot({

    # Made the complete Tree with ggtree 
    p_full <- ggtree(tree_boots) %<+% df_tip_data
    p_full <- p_full + geom_tiplab(offset = 0.6, hjust = 0.5) +
      geom_point(aes(shape = trophic_habit, color = trophic_habit,
                     size = mass_in_kg), na.rm = TRUE) +
      scale_shape_manual(values = trophic_shapes) +
      scale_color_manual(values = trophic_colors)+ scale_y_reverse()
    
    # Add highlight
    selected_node <- input$nodeSelect
    p_full <- p_full + geom_highlight(node = as.numeric(selected_node),
                                      fill = "NA", alpha = 0.01, extend = 1.5,
                                      color = "deepskyblue", linetype = 3)

    # Create the "zoom" tree
    zoom_node <- input$zoomNode
  
    p_clade <- viewClade(p_full, node = as.numeric(zoom_node)) 
    p_clade <- ggtree::rotate(p_clade,as.numeric(zoom_node)) 

    p_clade

  })
}

# Load Shiny App
shinyApp(ui = ui, server = server)

Image

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant