简体   繁体   中英

ToolTip on plot in shiny application

I would like to add a ToolTip for my x - axis and y -axis on my plot.

Here is my code:

# 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)

How could I create a tooltip like these one: 在此处输入图片说明

Or like these one: 在此处输入图片说明

Thanks.

You can use legends function to get the first result you deisre. I have modified your code below to do that:

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)

[EDIT]:

I have modified the code to get the x and y points of the plot but it is not exactly in the plot but outside.

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)

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM