如何将鼠标悬停在闪亮的ggplot2极坐标图中的标签上?

时间:2018-08-10 10:03:17

标签: r ggplot2 shiny plotly mouseover

我正在用鼠标悬停在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的输出。

任何建议如何将这些鼠标悬停在标签上?

1 个答案:

答案 0 :(得分:1)

以下是使用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 site

这是最终的Shiny代码。我没有太多使用Shiny,因此可以对其进行改进,但对我有用。

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