【问题标题】:ToolTip on plot in shiny application闪亮应用程序中绘图的工具提示
【发布时间】:2017-01-11 15:04:13
【问题描述】:

我想在我的绘图上为我的 x 轴和 y 轴添加一个工具提示。

这是我的代码:

# my data
data1=data.frame(absciss=c(0.00000000,0.08333333, 0.16666667, 0.25000000, 0.33333333, 0.41666667, 0.50000000, 0.58333333, 0.66666667, 0.75000000, 0.83333333, 0.91666667, 1.00000000, 1.00000000, 0.91666667, 0.83333333,0.75000000, 0.66666667, 0.58333333, 0.50000000, 0.41666667, 0.33333333, 0.25000000 ,0.16666667,0.08333333, 0.00000000),a=c(0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000 ,0.0000000,0.0000000 ,0.0000000 ,0.0000000 ,0.0000000, 0.0000000 ,0.0000000, 0.0000000, 0.3104093, 0.3673738, 0.4369944, 0.5223489, 0.6273225 ,0.7568293, 0.9171064 ,0.9366898, 0.9545479, 0.9702878 ,0.9834614, 0.9935587 ,1.0000000),b=c(1.0000000, 0.9935587, 0.9834614, 0.9702878, 0.9545479, 0.9366898, 0.9171064, 0.7568293, 0.6273225 ,0.5223489, 0.4369944, 0.3673738, 0.3104093, 5.3712182, 5.5752584, 5.7917893, 6.0230982, 6.2721479, 6.5427819, 6.8400031, 6.6000050, 6.3343244 ,6.0420686, 5.7225252, 5.3752221, 5.0000000),c=c(5.000000, 5.375222,5.722525 ,6.042069, 6.334324, 6.600005, 6.840003, 6.542782, 6.272148, 6.023098, 5.791789, 5.575258 ,5.371218, 5.735298 ,5.968894 ,6.217804, 6.484524, 6.772200, 7.084817, 7.427446, 7.239133, 7.030875, 6.802515, 6.554182, 6.286363 ,6.000000),d=c(6.000000 , 6.286363  ,6.554182,  6.802515,  7.030875,  7.239133,  7.427446 , 7.084817 , 6.772200 , 6.484524,  6.217804,  5.968894 , 5.735298 , 8.570569,  9.006399 , 9.473807,  9.976679, 10.519672, 11.108413, 11.749752 ,11.695621 ,11.616870 ,11.510890, 11.374844, 11.205658 ,11.000000))



 # Define UI for application that draws a plot
ui<-fluidPage(

  # Application title
  titlePanel("My Shiny Plot!"),

  sidebarLayout(
    sidebarPanel(),

    # Show the plot 
    mainPanel(
      plotOutput("Plot")
    )
  )
)

server<-function(input, output) {

  output$Plot <- renderPlot({
    plot(x=data1[,1],y=data1[,1],ylim=c(0,100),xlim=c(0,1),type="n")
        polygon(data1[,1],data1[,2],col=colors[1],border=F)
        polygon(data1[,1],data1[,3],col=colors[2],border=F)
        polygon(data1[,1],data1[,4],col=colors[3],border=F)
        polygon(data1[,1],data1[,5],col=colors[4],border=F)
  })
}
shinyApp(ui=ui, server=server)

我怎样才能创建一个像这样的工具提示:

或者像这些:

谢谢。

【问题讨论】:

    标签: r plot shiny tooltip polygon


    【解决方案1】:

    您可以使用 legends 函数来获得您想要的第一个结果。我已经修改了下面的代码来做到这一点:

    library(shiny)
    # my data
    data1=data.frame(absciss=c(0.00000000,0.08333333, 0.16666667, 0.25000000, 0.33333333, 0.41666667, 0.50000000, 0.58333333, 0.66666667, 0.75000000, 0.83333333, 0.91666667, 1.00000000, 1.00000000, 0.91666667, 0.83333333,0.75000000, 0.66666667, 0.58333333, 0.50000000, 0.41666667, 0.33333333, 0.25000000 ,0.16666667,0.08333333, 0.00000000),a=c(0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000 ,0.0000000,0.0000000 ,0.0000000 ,0.0000000 ,0.0000000, 0.0000000 ,0.0000000, 0.0000000, 0.3104093, 0.3673738, 0.4369944, 0.5223489, 0.6273225 ,0.7568293, 0.9171064 ,0.9366898, 0.9545479, 0.9702878 ,0.9834614, 0.9935587 ,1.0000000),b=c(1.0000000, 0.9935587, 0.9834614, 0.9702878, 0.9545479, 0.9366898, 0.9171064, 0.7568293, 0.6273225 ,0.5223489, 0.4369944, 0.3673738, 0.3104093, 5.3712182, 5.5752584, 5.7917893, 6.0230982, 6.2721479, 6.5427819, 6.8400031, 6.6000050, 6.3343244 ,6.0420686, 5.7225252, 5.3752221, 5.0000000),c=c(5.000000, 5.375222,5.722525 ,6.042069, 6.334324, 6.600005, 6.840003, 6.542782, 6.272148, 6.023098, 5.791789, 5.575258 ,5.371218, 5.735298 ,5.968894 ,6.217804, 6.484524, 6.772200, 7.084817, 7.427446, 7.239133, 7.030875, 6.802515, 6.554182, 6.286363 ,6.000000),d=c(6.000000 , 6.286363  ,6.554182,  6.802515,  7.030875,  7.239133,  7.427446 , 7.084817 , 6.772200 , 6.484524,  6.217804,  5.968894 , 5.735298 , 8.570569,  9.006399 , 9.473807,  9.976679, 10.519672, 11.108413, 11.749752 ,11.695621 ,11.616870 ,11.510890, 11.374844, 11.205658 ,11.000000))
    colors = c("red","orange", "green")
    
    
    # Define UI for application that draws a plot
    ui<-fluidPage(
    
      # Application title
      titlePanel("My Shiny Plot!"),
    
      sidebarLayout(
        sidebarPanel(),
    
        # Show the plot 
        mainPanel(
          plotOutput("Plot")
        )
      )
    )
    
    server<-function(input, output) {
    
      output$Plot <- renderPlot({
        plot(x=data1[,1],y=data1[,1],ylim=c(0,20),xlim=c(0,1),type="n")
        polygon(data1[,1],data1[,2],col=colors[1],border=F)
        polygon(data1[,1],data1[,3],col=colors[2],border=F)
        polygon(data1[,1],data1[,4],col=colors[3],border=F)
        polygon(data1[,1],data1[,5],col=colors[4],border=F)
        legend('topright', legend = colors, lty=1, col = colors, bty='n', cex=.75)
      })
    }
    shinyApp(ui=ui, server=server)
    

    [编辑]:

    我已经修改了代码以获取绘图的 x 和 y 点,但它并不完全在绘图中,而是在外部。

    library(shiny)
    # my data
    data1=data.frame(absciss=c(0.00000000,0.08333333, 0.16666667, 0.25000000, 0.33333333, 0.41666667, 0.50000000, 0.58333333, 0.66666667, 0.75000000, 0.83333333, 0.91666667, 1.00000000, 1.00000000, 0.91666667, 0.83333333,0.75000000, 0.66666667, 0.58333333, 0.50000000, 0.41666667, 0.33333333, 0.25000000 ,0.16666667,0.08333333, 0.00000000),a=c(0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000 ,0.0000000,0.0000000 ,0.0000000 ,0.0000000 ,0.0000000, 0.0000000 ,0.0000000, 0.0000000, 0.3104093, 0.3673738, 0.4369944, 0.5223489, 0.6273225 ,0.7568293, 0.9171064 ,0.9366898, 0.9545479, 0.9702878 ,0.9834614, 0.9935587 ,1.0000000),b=c(1.0000000, 0.9935587, 0.9834614, 0.9702878, 0.9545479, 0.9366898, 0.9171064, 0.7568293, 0.6273225 ,0.5223489, 0.4369944, 0.3673738, 0.3104093, 5.3712182, 5.5752584, 5.7917893, 6.0230982, 6.2721479, 6.5427819, 6.8400031, 6.6000050, 6.3343244 ,6.0420686, 5.7225252, 5.3752221, 5.0000000),c=c(5.000000, 5.375222,5.722525 ,6.042069, 6.334324, 6.600005, 6.840003, 6.542782, 6.272148, 6.023098, 5.791789, 5.575258 ,5.371218, 5.735298 ,5.968894 ,6.217804, 6.484524, 6.772200, 7.084817, 7.427446, 7.239133, 7.030875, 6.802515, 6.554182, 6.286363 ,6.000000),d=c(6.000000 , 6.286363  ,6.554182,  6.802515,  7.030875,  7.239133,  7.427446 , 7.084817 , 6.772200 , 6.484524,  6.217804,  5.968894 , 5.735298 , 8.570569,  9.006399 , 9.473807,  9.976679, 10.519672, 11.108413, 11.749752 ,11.695621 ,11.616870 ,11.510890, 11.374844, 11.205658 ,11.000000))
    colors = c("red","orange", "green")
    
    
    # Define UI for application that draws a plot
    ui<-fluidPage(
    
      # Application title
      titlePanel("My Shiny Plot!"),
    
      sidebarLayout(
        sidebarPanel(),
    
        # Show the plot 
        mainPanel(
          plotOutput("Plot", hover = "plot_hover"),
          verbatimTextOutput("info")
    
        )
      )
    )
    
    server<-function(input, output) {
    
      output$Plot <- renderPlot({
        plot(x=data1[,1],y=data1[,1],ylim=c(0,20),xlim=c(0,1),type="n")
        polygon(data1[,1],data1[,2],col=colors[1],border=F)
        polygon(data1[,1],data1[,3],col=colors[2],border=F)
        polygon(data1[,1],data1[,4],col=colors[3],border=F)
        polygon(data1[,1],data1[,5],col=colors[4],border=F)
        legend('topright', legend = colors, lty=1, col = colors, bty='n', cex=.75)
      })
    
    
      output$info <- renderText({
        paste0("x=", input$plot_hover$x, "\ny=", input$plot_hover$y)
      })
    }
    shinyApp(ui=ui, server=server)
    

    希望对你有帮助!

    【讨论】:

    • 您好 SBista,感谢您的回答。不幸的是,它没有回答我的问题。我想在我的图形中添加一个工具提示,当您在图形上移动鼠标时会显示 x 和 y 点。
    猜你喜欢
    • 2020-02-18
    • 1970-01-01
    • 2016-05-01
    • 2013-05-03
    • 2017-03-15
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多