Skip to content
R for the Rest of Us Logo

Mapping with R

34 lessons

mapview for Quick Maps (01_03)

This lesson is locked

Get access to all lessons in this course.

If the video is not playing correctly, you can watch it in a new window

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Learn More

As noted in the video, the mapview package does provide advanced customizability for the maps created. However, it’s recommended that you instead focus on using the leaflet package for publication-quality level interactive maps (the third section of this course). You can find the mapview package documentation here.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

perry carter

perry carter

February 12, 2022

What about tmap?

Irantzu Alvarez

Irantzu Alvarez

February 14, 2022

Hello, I am having problems with the instalation of 'mapview'. First, I install the package (install.packages("mapview")) and it works, but when I say 'library(mapview)' RStudio chrases and stop working. What could be the problem? Thank you.

Latif Apaassongo Ibrahim

Latif Apaassongo Ibrahim

February 17, 2022

Hi, I might be missing something. All map effects below, do show up in viewer pane of my RStudio. The map effects at: 2:56 and 4:00 (colored map of countries) 4:12 (pop-up menu upon clicking), 4:56 (rings around tiny countries) and 5:32 (colored map of states with the US). I will appreciate any assistance.

Thank you!!.

Charlie Hadley

Charlie Hadley

April 1, 2022

I'm glad it's working now!

Laura Coleman

Laura Coleman

October 29, 2022

I am having issues downloading the albersusa package from github. Here is the error that I receive: > remotes::install_github("hrbrmstr/albersusa") Downloading GitHub repo hrbrmstr/albersusa@HEAD Error in utils::download.file(url, path, method = method, quiet = quiet, : cannot open URL 'https://api.github.com/repos/hrbrmstr/albersusa/tarball/HEAD'

Is there a different way to install this package?

Hi Charlie - when trying to run alaska_landcover %>% mapview()

I got 2 error messages. The first said something about max pixel size and seemed to include a suggested solution in the error message, so I added maxpixels = 7194132, and that portion of the error isn't appearing.

However, I'm still getting this message and the map of Alaska is not loading: Error in levels<-(*tmp*, value = as.character(levels)) : factor level [3] is duplicated

Any ideas on this one? Thanks!!

Duncan McDonnell

Duncan McDonnell

December 29, 2023

Hi Charlie - I get this error when I try to install "albersusa":

remotes::install_github("hrbrmstr/albersusa") Downloading GitHub repo hrbrmstr/albersusa@HEAD Skipping 3 packages not available: maptools, rgdal, rgeos ── R CMD build ───────────────────────────────────────────────────────────────────────────────────────── ✔ checking for file ‘/private/var/folders/lb/r0v_lh112mv38lvxxg2k7mw8dg03d_/T/RtmpQ2tNGA/remotes7c78694cbb41/hrbrmstr-albersusa-07aa87f/DESCRIPTION’ ... ─ preparing ‘albersusa’: ✔ checking DESCRIPTION meta-information ─ checking for LF line-endings in source and make files and shell scripts ─ checking for empty or unneeded directories ─ building ‘albersusa_0.4.1.tar.gz’ Warning: invalid uid value replaced by that for user 'nobody' Warning: invalid gid value replaced by that for user 'nobody'

ERROR: dependencies ‘rgeos’, ‘rgdal’, ‘maptools’ are not available for package ‘albersusa’

  • removing ‘/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/albersusa’ Warning message: In i.p(...) : installation of package ‘/var/folders/lb/r0v_lh112mv38lvxxg2k7mw8dg03d_/T//RtmpQ2tNGA/file7c782c99de69/albersusa_0.4.1.tar.gz’ had non-zero exit status

library(albersusa) Error in library(albersusa) : there is no package called ‘albersusa’

What is it about? Thanks for your help!

David Keyes

David Keyes Founder

January 2, 2024

Hi Duncan! So, unfortunately, this is an issue because the rgeos, rgdal, and maptools packages were archived from CRAN. I'm checking with Charlie about the best approach here. I will say that you can do nearly the same thing with the tigris package using this code:

library(tigris)
library(mapview)

us_states <-
  states()

us_states |> 
  shift_geometry() |> 
  mapview()

The shift_geometry() function comes from the tigris package and makes it easier to see Alaska, Hawaii, and Puerto Rico.

David Keyes

David Keyes Founder

January 5, 2024

Quick update: Charlie says the best way is to use the tigris package in the way I shared. Hope that helps!

Andrew Paquin

Andrew Paquin

April 5, 2024

I'm having a lot of trouble with this (many of the same issues noted by others):

  1. albersusa is a non-starter. I used David's alternate (using tigris) and it seems to work.
  2. I can't get alaska_landcover to appear in mapview. I get this error/warning message combo: Error in levels<-(*tmp*, value = as.character(levels)) : factor level [3] is duplicated In addition: Warning message: In rasterCheckSize(x, maxpixels = maxpixels) : maximum number of pixels for Raster* viewing is 5e+05 ; the supplied Raster* has 7194132 ... decreasing Raster* resolution to 5e+05 pixels to view full resolution set 'maxpixels = 7194132 '
  3. When I run mapview(c(countries110, tiny_countries110)), I get this: Error in if (utils::file_test("-d", x)) { : the condition has length > 1. I did a bit of Googling and landed on this solution: combined_countries <- rbind(countries110, tiny_countries110) mapview(combined_countries). However, I received another error message indicating that the columns of the dataframes don't match. That's a lot of challenge for a single lesson. At this point, I'm not sure if I should continue with this course. Ideas?

David Keyes

David Keyes Founder

April 8, 2024

Ok, finally getting back to you on this.

  1. I'm glad my updated code worked.

  2. Unfortunately, the raster package no longer works as shown in the video. There have been a lot of changes in the geospatial world in R in recent years. The package that has superseded raster is called terra. You can install it with install.packages("terra") and then run this updated code to make this work:

library(rnaturalearthdata)
library(sf)
library(terra)
library(mapview)

alaska_landcover <- rast("data/alaska_landcover.img")

alaska_landcover |> 
  mapview()

However, if you aren't using raster data (and I doubt you are), I wouldn't even worry about it. For most people, vector data is what they work with when making maps.

  1. It seems like the syntax for working with the mapview() function changed when you want to display multiple layers. Instead of:
mapview(c(countries110, tiny_countries110))

You now need:

mapview(list(countries110, tiny_countries110))

I'm sorry about these issues. We're working on getting them updated in the course.