我尝试在geom_area上使用悬停工具提示,但我无法让它们使用该几何体。它只显示最低的分组变量集(在下面的示例中,它将显示' Lakers'悬停值,但不会显示'凯尔特人'。
有趣的是,如果用例如geom_point替换geom_area,下面的代码可以正常工作。但对于我制作的真实仪表板,区域图是必要的。
library("shiny")
library("ggplot2")
d <- data.frame(date = as.Date(c("2017-01-01", "2017-01-02", "2017-01-03",
"2017-01-01", "2017-01-02", "2017-01-03")),
team = c("Celtics", "Celtics", "Celtics",
"Lakers", "Lakers", "Lakers"),
points_scored = c(108, 89, 95, 78, 93, 82))
ui <- fluidPage(
mainPanel(
plotOutput("graph",
hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info")
)
)
server <- function(input, output) {
output$graph <- renderPlot({
ggplot(d, aes(x = date, y = points_scored, fill = team)) +
geom_area()
})
output$hover_info <- renderUI({
hover <- input$plot_hover
point <- nearPoints(d, hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
wellPanel(
paste0(point$team, " - ", point$date, ": ", point$points_scored)
)
})
}
runApp(list(ui = ui, server = server))
提前致谢!
---编辑---
它实际上是在不正确的位置显示悬停。见附图。它将点(1月1日的凯尔特人队)视为仍然在y = 108.我希望它悬停在可见红色条的顶部(108 + 78 = 186),但仍然显示108. / p>
答案 0 :(得分:0)
我找到了一个粗略的解决方法,但它让应用程序按照我的意愿去做。我编辑的应用程序只编辑服务器功能,如下所示:
该应用程序如下所示:
library(shiny)
library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)
d <- data.frame(date = as.Date(c("2017-01-01", "2017-01-02", "2017-01-03",
"2017-01-01", "2017-01-02", "2017-01-03")),
team = c("Celtics", "Celtics", "Celtics",
"Lakers", "Lakers", "Lakers"),
points_scored = c(108, 89, 95, 78, 93, 82))
ui <- fluidPage(
mainPanel(
plotOutput("graph",
hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info")
)
)
server <- function(input, output) {
output$graph <- renderPlot({
ggplot(d, aes(x = date, y = points_scored, fill = team)) +
geom_area()
})
output$hover_info <- renderUI({
d_workaround <- d %>%
spread(team, points_scored) %>%
mutate(Celtics = str_c(Celtics + Lakers, "-", Celtics),
Lakers = str_c(Lakers, "-", Lakers)) %>%
gather(team, points_scored, Celtics, Lakers) %>%
separate(points_scored, c("points_scored", "points_scored_real"), convert = TRUE)
hover <- input$plot_hover
point <- nearPoints(d_workaround, hover, threshold = 10, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
wellPanel(
paste0(point$team, " - ", point$date, ": ", point$points_scored_real)
)
})
}
runApp(list(ui = ui, server = server))