简体   繁体   中英

How to get mouse over labels in a shiny ggplot2 polar plot?

I'm struggeling with mouse over labels for my ggplot 2 polar plot in shiny.

Simple version of my code (without mouse over labels):

library(dplyr)
library(shiny)
library(ggplot2)

# Define UI for application that plots features of iris
ui <- fluidPage(
  br(),

  # Sidebar layout 
  sidebarLayout(

    # Inputs

    sidebarPanel( 
    ),

    # Outputs
    mainPanel(  
      plotOutput(outputId = "radarplot"), 
      br()
    )
  )
)

# Define server function required to create the radarplot
server <- function(input, output) { 

  # Create radarplot with iris dataset 
  output$radarplot  <- renderPlot ({ 
    iris %>%
      ggplot(.) + geom_histogram(aes(y = Petal.Width, x = Species, fill = Species), 
                                 binwidth= 1,
                                 stat= 'identity', 
                                 alpha = 1 ) + 
      geom_histogram(aes(y = Sepal.Width,  x = Species, fill = Species),  
                                 binwidth= 1, 
                                 stat= 'identity',
                                 alpha = 0.3) + 
      coord_polar() 


  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

I made a version using plotly, trying to add mouse over labels. But then I don't get a radar plot.

library(dplyr)
library(shiny)
library(ggplot2)
library(plotly)

# Define UI for application that plots features of iris
ui <- fluidPage(
  br(),

# Sidebar layout 
sidebarLayout(

# Inputs

    sidebarPanel( 
    ),

   # Outputs
      mainPanel(  
      plotlyOutput(outputId = "radarplot"), 
      br()
    )
  )
)

# Define server function required to create the radarplot
server <- function(input, output) { 

  # Create radarplot with iris dataset 
  output$radarplot  <- renderPlotly ({ 
    iris %>%
      ggplot(.) + geom_histogram(aes(y = Petal.Width, x = Species, fill = Species), 
                                 binwidth= 1,
                                 stat= 'identity', 
                                 alpha = 1 ) + 
      geom_histogram(aes(y = Sepal.Width,  x = Species, fill = Species),  
                                 binwidth= 1, 
                                 stat= 'identity',
                                 alpha = 0.3) + 
      coord_polar() 


  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

Ideally I want the mouse over label to give output about Petal.Width, Sepal.Width and Species when hovering over a particular Species 'wing'.

Any suggestions how to get these mouse over labels?

Here is an example of this using the ggiraph package. First the tooltip needs to be created.

library(tidyverse)
iris_group_means <- 
  iris %>% 
  group_by(Species) %>% 
  summarise_all(mean) %>% 
  mutate(tooltip = sprintf("Sepal Length: %1.2f\nSepal Width: %1.2f\nPetal Length: %1.2f\nPetal Width: %1.2f",
                           Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)) %>% 
  select(Species, tooltip)

Then this tooltip just needs to be provided as an aesthetic, and instead of geom_histogram , use the ggiraph::geom_histogram_interactive function.

my_gg <- 
  iris %>%
  ggplot() + 
  geom_histogram(aes(y = Petal.Width, x = Species, fill = Species), 
                                      binwidth= 1, 
                                      stat= 'identity', 
                                      alpha = 1 ) + 
  ggiraph::geom_histogram_interactive(aes(y = Sepal.Width,  x = Species, fill = Species, tooltip = tooltip),
                 binwidth= 1,
                 stat= 'identity',
                 alpha = 0.3) +
  coord_polar() 
ggiraph::ggiraph(code = print(my_gg))

This can then be used in Shiny. A few other steps are involved and there is a separate ggiraph::renderggiraph function to use. Details are on the ggiraph site

Here is the final Shiny code. I don't use shiny much so this can probably be improved upon, but it worked for me.

# Define UI for application that plots features of iris
ui <- fluidPage(
  br(),

  # Sidebar layout 
  sidebarLayout(

    # Inputs

    sidebarPanel( 
    ),

    # Outputs
    mainPanel(  
      ggiraph::ggiraphOutput(outputId = "radarplot"), 
      br()
    )
  )
)

# Define server function required to create the radarplot
server <- function(input, output) { 

  # Create radarplot with iris dataset 
  output$radarplot  <- ggiraph::renderggiraph ({ 
    iris_group_means <- 
      iris %>% 
      group_by(Species) %>% 
      summarise_all(mean) %>% 
      mutate(tooltip = sprintf("Sepal Length: %1.2f\nSepal Width: %1.2f\nPetal Length: %1.2f\nPetal Width: %1.2f",
                               Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)) %>% 
      select(Species, tooltip)

    iris <- 
      left_join(iris, iris_group_means, by="Species")

    my_gg <- 
      iris %>%
      ggplot() + 
      geom_histogram(aes(y = Petal.Width, x = Species, fill = Species), 
                     binwidth= 1, 
                     stat= 'identity', 
                     alpha = 1 ) + 
      ggiraph::geom_histogram_interactive(aes(y = Sepal.Width,  x = Species, fill = Species, tooltip = tooltip),
                                          binwidth= 1,
                                          stat= 'identity',
                                          alpha = 0.3) +
      coord_polar() 

    ggiraph::ggiraph(code = print(my_gg))


  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM