将鼠标悬停在 ggplot 散点图中

问题描述 投票:0回答:0

我试图在 ggplot 的散点图中创建悬停功能。虽然悬停对某些点有效,但对其他点无效(在 Chrome 或 RStudio 查看器窗格中运行时)。不知道为什么,非常感谢您的帮助。

library(car)   
library(shiny)
library(plotly)
library(shinydashboard)
library(tidyverse)

#Load data
data <- read.csv("DummyData_StackOverflow.csv", sep=";")
head(data)

#Set colors
colors<-c("#C36463", "#0B4141")

options(scipen = 999)

############UI
ui <- dashboardPage(
  dashboardHeader(title = "Title"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Measure", tabName = "measure", icon = icon("dashboard"))
      )
  ),
  dashboardBody(
    tabItems(
      # Measure content
      tabItem(tabName = "measure",
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(
        div(
          style = "position:relative",
          plotOutput("scatterplot", 
                     height = 250, hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
          uiOutput("hover_info"),
       ),
        width = 12)
      )
    )
  )
)
)

server <- function(input, output) {
    output$scatterplot <- renderPlot({
    scatterplot <- ggplot(data, aes( x=Level, y=Salary_EUR, color=Gender))+theme_classic()+
      geom_point(position = position_jitterdodge(), alpha=0.25) +scale_color_manual(values=colors)+
      theme(axis.text.x = element_text(color = "grey20", size = 15, angle = 0, hjust = .5, vjust = .5, face = "plain"),
            axis.text.y = element_text(color = "grey20", size = 15, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
            axis.title.x = element_text(color = "grey20", size = 15, angle = 0, hjust = .5, vjust = 0, face = "plain"),
            axis.title.y = element_text(color = "grey20", size = 15, angle = 90, hjust = .5, vjust = .5, face = "plain"),
            legend.title = element_text(size=15), 
            legend.text = element_text(size=15))+xlab("Level")+ylab("Salary")+ scale_y_continuous(labels = scales::comma)
    scatterplot<-scatterplot+ stat_summary(
      fun = mean, 
      geom = "errorbar", 
      aes(ymax = ..y.., ymin = ..y..), 
      position = position_dodge(width = 1), 
      width = 0.5,size=1.5)  
    scatterplot +ggtitle("")+theme(plot.title = element_text(color = "grey20", size = 15,  hjust = .5, vjust = .5, face = "plain"))
  })
    
    output$hover_info <- renderUI({
      hover <- input$plot_hover
      point <- nearPoints(data, hover, threshold = 5, maxpoints = 1, addDist = TRUE)
      if (nrow(point) == 0) return(NULL)
      
      left_px <- hover$coords_css$x
      top_px <- hover$coords_css$y
      
      # create style property for tooltip
      # background color is set so tooltip is a bit transparent
      # z-index is set so we are sure are tooltip will be on top
      style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
                      "left:", left_px + 2, "px; top:", top_px + 2, "px;")
      
      # actual tooltip created as wellPanel
      wellPanel(
        style = style,
        p(HTML(paste0("<b> Name: </b>", point$Name, "<br/>",
                      "<b> Level: </b>", point$Level, "<br/>",
                      "<b> Salary_EUR: </b>", point$Salary_EUR, "<br/>")))
      )
    })
}
shinyApp(ui, server)

在不同的浏览器和查看器窗格中尝试,也减少了分散的数据量。

ggplot2 shinydashboard
© www.soinside.com 2019 - 2024. All rights reserved.