0

I've been building a small scatterplot map in ggplot2 to post in a shiny app and it went ok, but I want to try and make it interactive, for example to see the values of each point and be able to resize the map as desired. I am seeing that there is a more simple solution rather than rebuilding another map in plotly, which is using ggplotly. The problem is that after I run ggplotly, I cannot view the map either in RStudio or Shiny, but no errors are appearing either.

Thank you in advance for the help and I apologize if the answer to this is obvious/simple, but I am not able to figure it.

UPDATE 1: Like A.Suliman said, It seems like the ggplotly map is generated but can be viewed only in a separate window rather than RStudio viewer. But it still doesn't appear in the app.

UPDATE 2: MLavoie pointed out that the code I provided works with R version 3.5.0., plotly 4.7.1.9000 & ggplot2 2.2.1.9000 (as in he can view the plot both in RStudio and Shiny dashboard). I currently use R 3.5.1., plotly 4.8.0 & ggplot2 3.0.0. To fix this issue I tried downgrading the packages to 4.7.1. and 2.2.1. using the CRAN repository, and it worked for a short time but than it began generating errors such as ggplot2 needs to use dev version for ggplotly (installing it updated me to the 3.0.0.900 version of course) and that shiny pkg needs update. I updated the packages to the dev version as well. I tried using R version 3.5.0 but that interferred with alot of packages and generated errors again. I went back and fourth with installing/removing different versions of the packages, but none seem to work.

My code

for ui.R

library(shinydashboard)
library(shiny)
library(plotly)
library(ggplot2)


dashboardPage(
  dashboardHeader(),
  dashboardSidebar(

    sidebarMenu(
      sidebarSearchForm(textId = "searchbar", buttonId = "searchbtn", label = "Search..."),
      menuItem("Test", tabName="state", icon = icon("dashboard")))
  ),

  dashboardBody(
    tabItems(

      tabItem(tabName="state",
              fluidRow(
                box(title = "Products across the country", width = 12, status = "success", plotlyOutput("map01"))
              )
       )
    )
  )
)

for server.R

library(shinydashboard)
library(shiny)
library(plotly)
library(ggplot2)

shinyServer(function(input, output) {

  world_map <- map_data("world")%>%filter(region=="Portugal")

  info = data.frame(read.csv("cities.csv", header = TRUE))

  dput(info)
  structure(list(name = structure(c(4L, 10L, 6L, 15L, 14L, 9L, 
  3L, 7L, 8L, 11L, 18L, 17L, 1L, 16L, 12L, 13L, 2L, 19L, 5L, 20L, 
  21L), .Label = c("Continente ID0912 - Espinho", "Continente ID8234 - 
  Guimaraes", "Continente ID9912 - Braga", "Continente ID9932 - Albufeira", 
  "Jumbo ID2441 - Lisbon", "Minipreco ID1120 - Almada", "Minipreco ID1150 - 
  Cacilhas", 
  "Minipreco ID1212 - Canelas", "Minipreco ID1231 - Beja", "Minipreco ID1332 
  - Alfena", "Minipreco ID5345 - Caparica", "Minipreco ID7772 - Fatima", 
  "Minipreco ID8723 - Gondomar", 
  "Minipreco ID8891 - Aveiro", "Pingodoce ID1002 - Amadora", "Pingodoce 
  ID4228 - Faro", "Pingodoce ID4778 - Cova da Piedade", "Pingodoce ID5426 - 
  Coimbra", "Pingodoce ID7734 - Lagos", "Pingodoce ID7734 - Nazare", 
  "Pingodoce ID9832 - Viana do Castelo"), class = "factor"), products = 
  c(86L, 53L, 77L, 89L, 61L, 65L, 60L, 43L, 72L, 34L, 41L, 88L, 44L, 23L, 
  67L, 87L, 45L, 56L, 19L, 
  87L, 53L), lat = c(37.09, 41.23, 38.68, 38.75, 40.65, 38.02, 
  41.55, 38.68, 41.08, 38.67, 40.22, 38.67, 41.01, 37.03, 39.62, 
  41.15, 41.44, 37.1, 38.72, 39.6, 41.71), long = c(-8.26, -8.52, 
  -9.16, -9.24, -8.66, -7.86, -8.43, -9.14, -8.59, -9.19, -8.43, 
  -9.15, -8.64, -7.94, -8.64, -8.52, -8.3, -8.68, -9.14, -9.06, 
  -8.83)), class = "data.frame", row.names = c(NA, -21L))

  output$map01<- renderPlotly({
    p = info %>% ggplot() + geom_polygon(data=world_map, aes(x=long, y=lat, group=group), fill="grey50", alpha=0.3) + geom_point(aes(x=long, y=lat, colour=products, size=products, group=name), alpha=0.6) + scale_size_continuous(range=c(1,10)) + theme_void() + coord_map() + guides(size=FALSE)
    ggplotly(p, tooltip = c("group","colour"))
  })  

})

This code creates the map if opened in a separate window, but won't show in Shiny.

cartita
  • 31
  • 9
  • In **Console** type p and hit enter as usual. In **Viewer** press show in new window "the last icon". New page will open in your default browser with your map. Why this happened, I have no idea. – A. Suliman Aug 24 '18 at 22:14
  • @A.Suliman How strange. Thank you for your answer! I tried it now and besides the fact that I can't view this in RStudio, when I open it in a new window and hover over the points, products appears twice, but the cities don't appear at all (i updated the post with example of the data I am using). Do you have any idea what might cause this? Thanks again for the help! – cartita Aug 24 '18 at 22:43
  • I used `info=world.cities %>% filter(country.etc=="Portugal")` to reproduce `info` and reproduce your map with `geom_point(data=info, aes(x=long, y=lat, colour=pop, size=pop), alpha=0.6)`. So in my case, yes indeed `pop` appear twice "I think because you used in colour and size"when I hover over points along with `lat` and `lan`. But if you check `?ggplotly` you will see that the default for `tooltip` is ''all". – A. Suliman Aug 24 '18 at 23:14
  • I'm not sure how great is this solution, but I added an extra argument to geom_point, which is group=name, and after that I modified the ggplotly call to ggplotly(p, tooltip = c("x","y","colour","group")) to eliminate the size component and add the name, and it seems to work. – cartita Aug 24 '18 at 23:50
  • I think its very good solution, also in the above mentioned tutorial they use a trick by creating a new column for text in hover using `mutate( mytext=paste("City: ", name, "\n", "Population: ", pop, sep=""))`, for more details you can check in the last map. And for future readrs, products/pop appear twice in the hover because we have two geoms namely `geom_polygon` and `geom_point`. – A. Suliman Aug 25 '18 at 00:01
  • Yes, I tried their solution but I couldn't get past the " text not recognized" aesthetic error, which is strange because I just reproduced their code with UK. I am ok with the version I have at the moment, but now my biggest fear is how to display it in Shiny dashboard, it's a big battle this one. Thank you so much for your time and advices! – cartita Aug 25 '18 at 00:43
  • Translating from ggplot to plotly is very ungrateful and hard. Once you have a prototype working, perhaps you could construct your plot using plotly only. That way, there's one less level of abstraction to debug through. – Roman Luštrik Aug 25 '18 at 06:02
  • Can you provide a reproducible example? You only showed some parts of your shiny code. We can't test your code as it is. – MLavoie Aug 25 '18 at 09:22
  • @RomanLuštrik Yes, I am seeing this is the best solution in the end. My problem was getting the map of Portugal and wasn't really sure how to do it in plotly, so it was a bit easier for me to do in in ggplot2. Thanks for the advice. – cartita Aug 25 '18 at 13:39
  • @MLavoie I updated my code. In this code, if I substitute plotly with plot (plotOutput("map01") & output$map01<- renderPlot) and remove the ggplotly sintax leaving just the plain ggplot2 one, the code works fine, the map appears in the web application. So this transition with ggplotly is problematic and I can't figure out why. Thanks for the help. – cartita Aug 25 '18 at 13:44
  • You also need to provide (with `dput()`) `cities.csv`. – MLavoie Aug 25 '18 at 13:51
  • @MLavoie I added it now. – cartita Aug 25 '18 at 14:14
  • I can see the map in shiny and rstudio. I am using `plotly_4.7.1.9000 ggplot2_2.2.1.9000`. – MLavoie Aug 25 '18 at 15:19
  • @MLavoie I initially had package 4.8.0 for plotly and 3.0.0. - I tried to install 4.7.1. and 2.2.1. from CRAN repository but an error appeared saying that I need to use dev version for ggplot2 to be able to work with ggplotly. After i installed it, it went to 3.0.0.900 and nothing worked with that, some elements from my cvs could not be found and so on. I tried updating the plotly to dev version and still doesn't work. I use R version 3.5.1. Where could I find the versions you have? Thank you again for the help. – cartita Aug 25 '18 at 17:12
  • @MLavoie what R version do you use? – cartita Aug 25 '18 at 20:08
  • `R version 3.5.0 (2018-04-23)`. – MLavoie Aug 25 '18 at 20:16
  • Do you have to use plotly? There are other mapping libraries which support interactive maps such as leaflet, mapdeck and googleway. – SymbolixAU Aug 28 '18 at 00:17

0 Answers0