Search code examples
rplotlymaps

Why does R chloropeth map display as a square?


I'm using R plotly (plotly_4.10.0) to create a chloropeth map of Europe. I'm using the R code present here. My output plot is always in the shape of a square. I've marked the borders in red in the image to highlight the issue.

How do I increase the width of my plot to show all of Europe without zooming out? I tried the options plot_ly(width = 800) and plot_ly(sizes = c(1, 600)) but that does not make a difference.

library(plotly)
trace1 <- list(
  geo = "geo", 
  type = "choropleth", 
  z = c("6", "4", "5"), 
  showscale = TRUE, 
  locationmode = "country names", 
  locations = c("Germany", "France", "Italy"), 
  autocolorscale = TRUE
)
data <- list(trace1)
layout <- list(
  geo = list(
    scope = "europe", 
    domain = list(
      x = c(0, 1), 
      y = c(0, 1)
    ), 
    lataxis = list(range = c(35.0, 70.0)), 
    lonaxis = list(range = c(-9.0, 38.0)), 
    showland = TRUE, 
    landcolor = "rgb(229, 229, 229)", 
    showframe = TRUE, 
    projection = list(type = "Mercator"), 
    resolution = 50, 
    countrycolor = "rgb(255, 0, 255)", 
    coastlinecolor = "rgb(0, 255, 255)", 
    showcoastlines = TRUE
  ), 
  title = "map of Europe", 
  legend = list(traceorder = "reversed")
)
p <- plot_ly()
p <- add_trace(p, geo=trace1$geo, type=trace1$type, z=trace1$z, showscale=trace1$showscale, locationmode=trace1$locationmode, locations=trace1$locations, autocolorscale=trace1$autocolorscale)
p <- layout(p, geo=layout$geo, title=layout$title, legend=layout$legend)

enter image description here

enter image description here


Solution

  • You could play with the margin like this:

    library(plotly)
    trace1 <- list(
      geo = "geo", 
      type = "choropleth", 
      z = c("6", "4", "5"), 
      showscale = TRUE, 
      locationmode = "country names", 
      locations = c("Germany", "France", "Italy"), 
      autocolorscale = TRUE
    )
    data <- list(trace1)
    layout <- list(
      geo = list(
        scope = "europe", 
        domain = list(
          x = c(0, 1), 
          y = c(0, 1)
        ), 
        lataxis = list(range = c(35.0, 70.0)), 
        lonaxis = list(range = c(-9.0, 38.0)), 
        showland = TRUE, 
        landcolor = "rgb(229, 229, 229)", 
        showframe = TRUE, 
        projection = list(type = "Mercator"), 
        resolution = 50, 
        countrycolor = "rgb(255, 0, 255)", 
        coastlinecolor = "rgb(0, 255, 255)", 
        showcoastlines = TRUE
      ), 
      title = "map of Europe", 
      legend = list(traceorder = "reversed")
    )
    m <- list(
      l = 200,
      r = 200,
      b = 50,
      t = 50,
      pad = 40
    )
    p <- plot_ly()
    p <- add_trace(p, geo=trace1$geo, type=trace1$type, z=trace1$z, showscale=trace1$showscale, locationmode=trace1$locationmode, locations=trace1$locations, autocolorscale=trace1$autocolorscale)
    p <- layout(p, geo=layout$geo, title=layout$title, legend=layout$legend,
                margin = m)
    p
    #> Warning: `marker.color` does not currently support multiple values.
    

    enter image description here

    Created on 2022-09-30 with reprex v2.0.2