Skip to content
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

drill down treemap in shiny? #22

Closed
dockstreet opened this issue Jan 27, 2018 · 1 comment
Closed

drill down treemap in shiny? #22

dockstreet opened this issue Jan 27, 2018 · 1 comment

Comments

@dockstreet
Copy link

dockstreet commented Jan 27, 2018

Is there a way to drill down into subgroups with treemaply in shiny ? perhaps by clicking on a tooltip with an html link to observe or tile to expand?

Would this approach work?
https://stackoverflow.com/questions/45021775/use-on-click-for-a-treemapify-object-in-r-shiny

I've seen treemap, on the pokemon dashboard which does support a drilldown
http://jkunst.com/flexdashboard-highcharter-examples/pokemon/

hoping that treemapify would have similar behavior or could be achieved with a click to post back to shiny

@dockstreet dockstreet changed the title drill down treemap? drill down treemap in shiny? Jan 27, 2018
@wilkox
Copy link
Owner

wilkox commented Jan 30, 2018

I'm not a Shiny expert but here's an approach that seems to work:

library(ggplot2)
library(shiny)
library(tidyverse)
library(treemapify)

ui <- shinyUI(fluidPage(
  titlePanel("Drill-down treemap"),
  mainPanel(
    plotOutput("graph", width = "100%", click = "tclick"),
    actionButton("zoomout", "Zoom out")
  )
))

server <- shinyServer(function(input, output, session) {

  data <- G20
  makeReactiveBinding('data')

  output$graph <- renderPlot({
    ggplot(data, aes(area = gdp_mil_usd, fill = hdi, subgroup = region, label = country)) +
      geom_treemap() +
      geom_treemap_text(reflow = T) +
      geom_treemap_subgroup_text(grow = T, alpha = 0.5) +
      geom_treemap_subgroup_border()
  })  

  # When the 'Zoom out' button is clicked, reset the treemap
  observeEvent(input$zoomout, {
    data <<-
  })

  # When a subgroup is clicked
  observeEvent(input$tclick, {

    # Calculate coordinates of main treemap
    coords <- treemapify(data, area = "gdp_mil_usd", fill = "hdi", label =
                         "country", group = "region",  xlim = c(0, 1), ylim =
                         c(0, 1))

    # Figure out which tile was clicked and what subgroup it belongs to
    clickedSubgroup <- coords %>%
      filter(xmin < input$tclick$x) %>%
      filter(xmax > input$tclick$x) %>%
      filter(ymin < input$tclick$y) %>%
      filter(ymax > input$tclick$y) %>%
      .$group

    # Subset data to only that subgroup
    data <<- data %>% filter(region == clickedSubgroup)
  })
})
shinyApp(ui=ui,server=server)

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

2 participants