闪亮应用程序

时间:2017-01-11 15:04:14

标签: r plot shiny tooltip polygon

我想在我的情节中为我的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)

我怎样才能创建像这样的工具提示: enter image description here

或者喜欢这些: enter image description here

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用图例功能获取您所需的第一个结果。我已修改下面的代码来执行此操作:

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)

希望它有所帮助!