2

I hope you can help me. I have created a choropleth Map with Leaflet. I merged my (dataframe with countries and a random score) and a Shapefile with the Polygon data. So far it is working, however if I implement it in R-Shiny, the map is showing, but with no color. There is also no error showing. Anyone knows why?

My code:

ui <- fluidPage(
  leafletOutput("map")
)


shinyServer(function(input, output) {

output$map <- renderLeaflet({
    test_map
  })
})

global.R

tmp <- tempdir()

url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"

file <- basename(url)

download.file(url, file)

unzip(file, exdir = tmp)

world <- readOGR(dsn = tmp, layer = "ne_50m_admin_0_countries", encoding = "UTF-8")

data <- data.frame(Code = c("AR", "AU", "BE", "BR"),
             Score = c(0.01, -0.05, 0.15, -0.22))

world <- merge(world, data,
               by.x = "iso_a2",
               by.y = "Code",
               sort = FALSE)

pal <- colorNumeric(
  palette = "RdYlGn",
  domain = world$Score
)

test_map <- leaflet(data = world) %>%
            addTiles() %>% 
            addPolygons(fillColor = ~pal(Score), 
                        fillOpacity = 0.9, 
                        color = "#BDBDC3", 
                        weight = 1)
Josef
  • 21
  • 4
  • Is there a way to make this [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)? – alistaire May 05 '16 at 20:14
  • Hi @alistaire, I added some reproducible code ;). Thanks in advance. – Josef May 05 '16 at 20:21
  • when I run your code, I see three colors (Argentina, Brasil, and Australia) – MLavoie May 05 '16 at 21:44
  • @MLavoie You are right. Thats a strange behaviour. When I run this code alone, then it is working. However, if I run this code within my whole code, the colors are not showing. It seems that some function of packages overwrite some of the leaflet functions? – Josef May 06 '16 at 09:04
  • It seems that the function addPolygons is ignored :/. – Josef May 06 '16 at 09:32
  • maybe you should include in your post your whole code, if not too long; enough to reproduce your problem – MLavoie May 06 '16 at 14:18
  • @MLavoie unfortunately the code is very long :(. I just don´t understand the problem. There is no error showing. The map is showing, just the "addPolygons" function seems to be ignored. – Josef May 07 '16 at 09:59

1 Answers1

1

I know this is an old question and I'm not sure whether this will help or not, but I believe I had a similar problem to you which was just solved.

In my case, I had no trouble displaying polygon colours within Rstudio on my own PC, but certain web browsers and older versions of Rstudio refused to fill polygons with colours, even though all other aspects of the map worked fine.

The problem was that my colour palette consisted of a vector of hex codes with an alpha channel (the last couple of digits, specifying transparency). Removing the alpha channel from the hex codes solved my problem. It may be worth checking whether your colour vectors include alpha and if so, removing it with something like gsub(".{2}$","",your_colour_vector) as per the answer to my own problem (link above).

It doesn't look like your colours include alpha in your sample code but maybe it's a problem in your full code. That would explain why the sample code works but the full code doesn't. Might be something to look into anyway? Sorry I can't help more, I know this is a bit of a shot in the dark and not a full solution.

Community
  • 1
  • 1
JaydenM-C
  • 289
  • 1
  • 9