4

I am trying to write a script that would allow people to click on a country then a popup would appear and show the country's name and corresponding value. I have successfully created the map and the boundaries of each country, however when you click on, for example Russia, the popup would show India. Below is a simplified version of the script:

library(leaflet)
library(maps)

countries <- c("Australia", "South Africa", "India", "Mexico", "USA", "Russia")

values <- c(1,2,3,4,5,6)

bounds <- map("world", countries, fill = TRUE, plot = FALSE)


map <- leaflet(data) %>%
       addTiles() %>%
       addPolygons(data = bounds, group = "Countries", 
                   color = "red", 
                    weight = 2,
                    popup = paste("Country: ", countries, "<br>", "Value: ", values, "<br>"),
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

map

The map is generated using the leaflet package in R. Any solutions or advice would be welcome, I am sure it is a simple error that I am making somewhere.

  • If you replace `popup = countries` to `popup = ~names`, you will see the names of the countries. If you really have to show the names exactly you specified in `countries`, you need to do some string manipulation. – jazzurro Mar 27 '18 at 14:04
  • Thank you that works. But what if I want to append some other information as well? Like info on the region based on data I acquired? –  Mar 27 '18 at 14:15
  • I cannot see how you want to have popups. Do you wanna show country and region together? – jazzurro Mar 27 '18 at 14:19
  • Sorry that was unclear. If I click on Russia it should have a popup showing the country, population size, average temperatures etc.. –  Mar 27 '18 at 14:22

1 Answers1

2

I leave two options for you. If you just want to show the country names as they are in bounds (e.g., Australia:Melville Island), you need to change popup = countries to popup = ~names.

map1 <- leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        addPolygons(data = bounds, group = "Countries", 
                    color = "red", 
                    weight = 2,
                    popup = ~names,
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

enter image description here

If you want to just have the country names as you specified in countries, you want to manipulate names using gsub(), for example. Here I removed all characters from : to the end for each name.

map2 <- leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        addPolygons(data = bounds, group = "Countries", 
                    color = "red", 
                    weight = 2,
                    popup = ~gsub(x = names, pattern = ":.*$", replacement = ""),
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

enter image description here

EXTRA

The OP added one more thing to his question. Here is my idea. If you have two things to show in popups, you can do the following. You add value to bounds and create popups.

# Add values to bounds. 

set.seed(111)
bounds$value <- sample.int(n = 1000, size = 301, replace = TRUE)

map3 <- leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        addPolygons(data = bounds, group = "Countries", 
                    color = "red", 
                    weight = 2,
                    popup = paste("Country: ", bounds$names, "<br>",
                                  "Value: ", bounds$value, "<br>"),
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Thank you, that works well. But what if, for example, I have the list of countries and with them I also want information regarding data I obtained to also be shown in the popup. data <- c(20, 30, 33, 12, 34, 55) –  Mar 27 '18 at 14:21
  • @RoelofCoertze Have a look of [**this post**](https://stackoverflow.com/questions/47571337/how-to-create-a-choropleth-on-a-leaflet-map-r), for example. – jazzurro Mar 27 '18 at 14:24
  • please see edited version, the post you linked helped, but did not solve the problem. –  Mar 27 '18 at 14:51
  • @RoelofCoertze I gotta go to bed soon since it is pretty late here. One thing you want to realize is that I did not use the vector, `countries`. I rather used `names` which is a part of `bounds`. If you have `value` in `bounds`, I think my approach in the link would probably work. Otherwise, you would need to create a data frame including the country names (i.e., `names`) and values. In short, using a vector like `countries` and `values` is not a right way. – jazzurro Mar 27 '18 at 15:00
  • No problem I will try that. I am not a trained programmer, just a humble biologist so this is pretty new. –  Mar 27 '18 at 15:04
  • @RoelofCoertze I added the extra thing. Have a look. Good luck with your work. – jazzurro Mar 27 '18 at 23:16
  • @RoelofCoertze Pleasure to help you. – jazzurro Mar 28 '18 at 07:50