在R Shiny

时间:2019-06-07 16:47:41

标签: r ggplot2 shiny label multi-index

我想在主面板上以cumtom格式显示多索引,但该代码无法正常工作。

ui <-   bootstrapPage(
  titlePanel("Population Variation"),

  sidebarPanel(
    sliderInput("time", "year",2013, 
                2017,
                value = 2013,
                step=1,
                animate=T)
  ),

  mainPanel(
    tabsetPanel(type = "tabs",
                tabPanel("Map", 
                         tags$head(tags$style(type = "text/css", "#mymap {height:85vh !important;}")),
                         # this is an extra div used ONLY to create positioned ancestor for tooltip
                         # we don't change its position
                         div(
                           style = "position:relative",
                           plotOutput("mymap", 
                                      hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
                           uiOutput("hover_info")
                         ),
                         width = 7
                ))
)
)

server <- function(input, output, session) {


  output$mymap <- renderPlot({


    dataFilter <- subset(map_aus_df3, map_aus_df3$Year == as.numeric(input$time))



    ggplot() +
      geom_map(data = dataFilter, map = dataFilter, aes(x = long, y = lat, map_id = id),
               color = "#2b2b2b", fill = NA) +
      geom_map(data = dataFilter, map = dataFilter, aes(fill = (Population/Migrants), alpha = (Population + Migrants), map_id = id), color = 'gray50', size = 0.1) +
      scale_fill_viridis()+guides(alpha=F,fill=F)+

      theme(plot.title=element_text(size = 16, face="bold",margin=margin(b=10))) +
      with(distcenters, annotate(geom="text", x = clong, y = clat, label=id, size=4)) +
      theme_bare +
      labs(title="Total Population and Migrant Growth")

    vp<-viewport(width=0.24,height=0.24,x=0.58,y=0.14)
    print(g.legend+labs(title=""),vp=vp)

  })

  output$hover_info <- renderUI({
    dataFilter <- subset(map_aus_df3, map_aus_df3$Year == as.numeric(input$time))

    hover <- input$plot_hover
    point <- nearPoints(dataFilter, hover, threshold = 10, maxpoints = 1, addDist = TRUE)

    if (nrow(point) == 0) return(NULL)

    # calculate point position INSIDE the image as percent of total dimensions
    # from left (horizontal) and from top (vertical)
    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)

    # calculate distance from left and bottom side of the picture in pixels
    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)

    # create style property fot tooltip
    # background color is set so tooltip is a bit transparent
    # z-index is set so we are sure are tooltip will be on 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;")

    # actual tooltip created as wellPanel
    wellPanel(
      style = style,
      p(HTML(paste0("<b> State: </b>", point$id, "<br/>",
                    "<b> Migrant Population: </b>", point$Migrants, "<br/>",
                    "<b> Total Population: </b>", point$Population, "<br/>")))
    )
  })

}

shinyApp(ui, server)

这是我到目前为止尝试过的。如果我将ggplot行放在打印功能中,则近点会给出无法识别xVar的错误。我已经为此奋斗了几个小时,并非常感谢您的帮助。谢谢

0 个答案:

没有答案