繁体   English   中英

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

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

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

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

这是使用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网站上

这是最终的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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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