首页 > 解决方案 > 如何将鼠标悬停在闪亮的 ggplot2 极坐标图中的标签上?

问题描述

我正在为我的 ggplot 2 极坐标图的标签而苦苦挣扎。

我的代码的简单版本(没有鼠标悬停在标签上):

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)

我使用 plotly 制作了一个版本,试图在标签上添加鼠标。但是我没有得到雷达图。

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)

理想情况下,当鼠标悬停在特定物种“翼”上时,我希望鼠标悬停在标签上以提供有关 Petal.Width、Sepal.Width 和 Species 的输出。

有什么建议如何让这些鼠标悬停在标签上?

标签: rggplot2shinyplotlymouseover

解决方案


这是使用ggiraph包的示例。首先需要创建工具提示。

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)

那么这个工具提示只需要作为一种美学来提供,而不是geom_histogram使用该ggiraph::geom_histogram_interactive功能。

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))

然后可以在 Shiny 中使用它。涉及其他一些步骤,并且有一个单独的ggiraph::renderggiraph功能可供使用。详细信息在ggiraph 网站上

这是最终的闪亮代码。我很少使用闪亮的东西,所以这可能会有所改进,但它对我有用。

# 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)

推荐阅读