0

Please note that I cannot use leaflet, since I need to add custom shapes to the map which I cannot do in leaflet.

So the below is similar what I have in mind, where I have a ggmap with some extra geomPolygon layers added to it, and then it becomes the entire background of the R Shiny page with the widgets sitting on top of it.

enter image description here

  • "Please note that I cannot use leaflet, since I need to add custom shapes to the map which I cannot do in leaflet." This is #fakenews – cory Jun 28 '19 at 15:40
  • @cory If you know how to do that, then can you provide an alternative answer to this question that does not require the need to save the custom shape as a png file and then import it? https://stackoverflow.com/questions/56794478/how-do-i-add-custom-shapes-to-a-leaflet-map-in-r – Bear Bile Farming is Torture Jun 28 '19 at 15:58
  • Well, if you are using leaflet, then you cannot combine ggplot objects nicely with it unless it's done as suggested as an image file. You can use ESRI shapefiles natively in leaflet. It looks like this SO question would get you most of the way there... https://stackoverflow.com/questions/46248061/save-a-ggplot2-coord-map-chart-in-shapefile – cory Jun 28 '19 at 17:21

1 Answers1

2

I do not have a ggmap API Key, but it works the same way. You all do that with CSS.

You have to set the body height and width of 100%, same applies for the plot so they extend to the width and height of the viewport.

Everything on top of that has to be set to position: absolute. This means the div will be 10px away from the top and 10px away from the right. absolutePanel does this setting for you, but you could do it in your own css.

library(shiny)
library(leaflet)
library(RColorBrewer)
library(ggmap)


ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  plotOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                sliderInput("range", "Magnitudes", min(quakes$mag), max(quakes$mag),
                            value = range(quakes$mag), step = 0.1
                ),
                selectInput("colors", "Color Scheme",
                            rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                ),
                checkboxInput("legend", "Show legend", TRUE)
  )
)

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

  output$map <- renderPlot({
    df <- data.frame(
      gp = factor(rep(letters[1:3], each = 10)),
      y = rnorm(30)
    )
    ds <- do.call(rbind, lapply(split(df, df$gp), function(d) {
      data.frame(mean = mean(d$y), sd = sd(d$y), gp = d$gp)
    }))


    ggplot(df, aes(gp, y)) +
      geom_point() +
      geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)
  })

}

shinyApp(ui, server)
DSGym
  • 2,807
  • 1
  • 6
  • 18