I tried to create a hover over functionality in a scatter in ggplot. While hover over works for some dots, it does not for others (see Scatter without hover-over - both in Chrome or RStudio viewer pane). No idea why it works for some and for others not, any help is highly appreciated. Data follows below format as I created a grouped scatter based on gender.
Name,Gender,Age,Level,Performance,Salary_EUR
Ellen Eugenia Johnson Sirleaf,Female,44,6,32,39132
Jimmy Carter,Male,41,7,36,38902
Aung San Suu Kyi,Female,55,1,65,373655
Barack Obama,Male,57,1,59,299145
library(car)
library(shiny)
library(plotly)
library(shinydashboard)
library(tidyverse)
#Load data
data <- read.csv("DummyData.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)
Tried in different browser and Viewer pane, also reduced amount of data in scatter.