Get startedGet started for free

Geo layout

In the previous exercise you saw the default settings for the geo layout in plotly, but this it is quite easy to customize by specifying additional arguments in the list passed to geo in layout().

In this exercise you will explore a few useful options outlined below:

  • To change the color of the landmass, add the arguments showland = TRUE and set a landcolor.
  • To make lakes distinct from landmasses, add the arguments showlakes = TRUE and set a lakecolor.
  • To display states/provinces, set showsubunit = TRUE, and the set subunitcolor.
  • To display countries, set showcountries = TRUE, and the set countrycolor.

Note that you must use the toRGB() function in order to pass R colors to the geo layout.

plotly has already been loaded for you.

This exercise is part of the course

Interactive Data Visualization with plotly in R

View Course

Exercise instructions

  • Customize the appearance of your map from the previous exercise by defining the list g and passing it to the geo layout:
    • Set the landmass color with "gray90".
    • Set the lake color with "white".
    • Set the state (subunit) color with "white".

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Customize the geo layout
g <- list(scope = 'usa', 
          showland = ___, landcolor = ___,
          showlakes = ___, lakecolor = ___,
          showsubunit = ___, subunitcolor = ___)

# Apply the geo layout to the map
rallies2018 %>%
  plot_geo(locationmode = 'USA-states') %>%
  add_markers(
    x = ~long, y = ~lat, size = ~no.speakers, 
    hoverinfo = "text", text = ~paste(city, state, sep = ",")
  ) %>%
  layout(title = "2018 Trump Rallies", geo = ___)
Edit and Run Code