Hav*_*v11 0 r tooltip ggplot2 shiny
当我将鼠标悬停在图表中的一个点上时,我希望显示高度或重量的值。我已经尝试通过使用plotly包和此链接的示例来完成这项工作。但是我遇到了各种各样的错误,我不知道如何使它工作。
我已经包含了我的整个代码,所以我希望有人可以帮助我解决这个问题。
library("shiny")
library("ggplot2")
library('readxl')
library('gridExtra')
ui<- fluidPage(
titlePanel("Animals"),
sidebarLayout(
sidebarPanel(
helpText("Create graph of height and/or weight animals"),
selectInput("location",
label = "Choose a location",
choices = list("New York"="New York", "Philadelphia" = "Philadelphia"),
selected = "New York"),
uiOutput("animal"),
checkboxGroupInput("opti",
label = "Option",
choices = c("weight", "height"),
selected = "weight")
),
mainPanel(plotOutput("graph"))
))
server <- function(input, output){
animal <- read_excel('data/animals.xlsx', sheet =1)
var <- reactive({
switch(input$location,
"New York" = list("Cat1", "Dog2"),
"Philadelphia"= list("Cat4","Dog3"))
})
output$animal <- renderUI({
checkboxGroupInput("anim", "Choose an animal",
var())
})
output$graph <- renderPlot({
if (length(input$anim)==1){
p <- ggplot(subset(animal, Name %in% input$anim & Location %in% input$location), aes(x=date))
if ("weight" %in% input$opti){
p <- p + geom_line(aes(y=weight)) + geom_point(aes(y=weight))
}
if ("height" %in% input$opti){
p <- p + geom_line(aes(y=height)) + geom_point(aes(y=height))
}
print(p)
}
if (length(input$anim)==2){
p1 <- ggplot(subset(animal, Name %in% input$anim[1] & Location %in% input$location), aes(x=date))
p2 <- ggplot(subset(animal, Name %in% input$anim[2] & Location %in% input$location), aes(x=date))
if ("weight" %in% input$opti){
p1 <- p1 + geom_line(aes(y=weight)) + geom_point(aes(y=weight))
p2 <- p2 + geom_line(aes(y=weight)) + geom_point(aes(y=weight))
}
if ("height" %in% input$opti){
p1 <- p1 + geom_line(aes(y=height)) + geom_point(aes(y=height))
p2 <- p2 + geom_line(aes(y=height)) + geom_point(aes(y=height))
}
grid.arrange(p1,p2, ncol = 2)
}
})
}
shinyApp(ui=ui, server= server)
Run Code Online (Sandbox Code Playgroud)
部分数据:
Location Name date weight height
New York Cat1 Mar-16 34,20 22,50
New York Cat1 Apr-16 35,02 23,02
New York Cat1 May-16 35,86 23,55
New York Cat1 Jun-16 36,72 24,09
New York Dog2 Mar-16 33,55 22,96
New York Dog2 Apr-16 33,62 23,42
New York Dog2 May-16 33,68 23,89
New York Dog2 Jun-16 33,75 24,37
Philadelphia Cat4 Mar-16 20,33 16,87
Run Code Online (Sandbox Code Playgroud)
我使用了这个工具提示并对其进行了一些自定义。
您的情节最初不会出现,因为您没有返回任何情节。我返回一个 ggplot 对象p而不调用print函数。
一般来说,我大量修改了您的代码,结果如下:
由于该函数nearPoints需要您传递给 ggplot 的相同数据集,因此我必须创建一个新的reactive,在其中对您的数据进行了一些子集化和整形。
grid.arrange我使用的不是创建两个单独的图facet_grid(因此我必须转换数据)。我还使用颜色来区分线条。
使用您提供的示例数据一切正常。
完整示例:
rm(ui)
rm(server)
library("shiny")
library("ggplot2")
library('readxl')
library('gridExtra')
library(reshape) # for "melt"
ui<- fluidPage(
titlePanel("Animals"),
sidebarLayout(
sidebarPanel(
helpText("Create graph of height and/or weight animals"),
selectInput("location",
label = "Choose a location",
choices = list("New York"="New York", "Philadelphia" = "Philadelphia"),
selected = "New York"),
uiOutput("animal"),
checkboxGroupInput("opti",
label = "Option",
choices = c("weight", "height"),
selected = "weight")
),
mainPanel(
# this is an extra div used ONLY to create positioned ancestor for tooltip
# we don't change its position
div(
style = "position:relative",
plotOutput("graph",
hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info")
)
)
))
server <- function(input, output){
animal <- read_excel('data/animals.xlsx', sheet =1)
#animal <- read_excel("~/Downloads/test2.xlsx")
var <- reactive({
switch(input$location,
"New York" = c("Cat1", "Dog2"),
"Philadelphia"= c("Cat4","Dog3"))
})
output$animal <- renderUI({
checkboxGroupInput("anim", "Choose an animal",
var())
})
output$graph <- renderPlot({
req(input$anim, sub())
if (length(input$anim) == 1) {
p <- ggplot(sub(), aes(x = date, colour = variable))
p <- p + geom_line(aes(y = value)) +
geom_point(aes(y = value)) +
guides(colour = guide_legend(title = NULL))
return(p) # you have to return the plot
}
if (length(input$anim) == 2) {
p <- ggplot(sub(), aes(x = date, colour = variable)) +
geom_line(aes(y = value)) +
geom_point(aes(y = value)) +
facet_grid(~ Name) +
guides(colour = guide_legend(title = NULL))
return(p) # you have to return the plot
}
})
observe({
print(sub())
})
sub <- reactive({
req(input$anim)
if (length(input$anim) == 1) {
df <- animal[animal$Name %in% input$anim & animal$Location %in% input$location, ]
df <- melt(as.data.frame(df), measure.vars = c("weight", "height"))
df <- subset(df, df$variable %in% input$opti)
return(df)
}
if (length(input$anim) == 2) {
df <- animal[animal$Name %in% input$anim & animal$Location %in% input$location, ]
df$Name <- factor(df$Name)
df <- melt(as.data.frame(df), measure.vars = c("weight", "height"))
df <- subset(df, df$variable %in% input$opti)
return(df)
}
})
output$hover_info <- renderUI({
hover <- input$plot_hover
point <- nearPoints(sub(), hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
left_pct <- (hover$x - hover$domain$left) / (hover$domain$right - hover$domain$left)
top_pct <- (hover$domain$top - hover$y) / (hover$domain$top - hover$domain$bottom)
left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$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;")
wellPanel(
style = style,
p(HTML(paste0("<b>", point$variable, ": </b>", point$value)))
)
})
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4818 次 |
| 最近记录: |