Back to Course
Mapping with R
0% Complete
0/0 Steps
-
Welcome to Mapping with R (01_01)
-
Geospatial DataMaking Maps is Complex (01_02)
-
mapview for Quick Maps (01_03)
-
sf for Simple Features (01_04)
-
Turning Data Frames into sf Objects (01_05)
-
Importing Shapefiles (01_06)
-
Joining Geospatial Datasets (01_07)
-
Disambiguating Country Names (01_08)
-
Converting Addresses to Coordinates (01_09)
-
U.S.-Specific Datasets (01_10)
-
Advice on Finding International Datasets (01_11)
-
CRS and Projections: Geographic and Projected CRS (01_12)
-
CRS and Projections: How to Choose a CRS (01_13)
-
Introducing Raster GIS with raster and stars (01_14)
-
Basics of Using the raster Package (01_15)
-
Static Mapsggplot2 Essentials (02_01)
-
Starting a Map in ggplot2 (02_02)
-
Labelling ggplot2 Maps (02_03)
-
Compare Locations/Events with Geobubble Charts (02_04)
-
Highlight a Region in a Country with ggplot2 (02_05)
-
Make a Choropleth Map of Discrete Variables with ggplot2 (02_06)
-
Make a Choropleth Map of Continuous Variables with ggplot2 (02_07)
-
Faceting Choropleth Maps with ggplot2 (02_08)
-
Visualize Raster Data with ggplot2 (02_09)
-
Adding Scale Bars and North Arrows with ggplot2 (02_10)
-
Interactive MapsWhat is leaflet? (03_01)
-
Starting a Map in leaflet (03_02)
-
Necessary HTML for Labelling leaflet Maps (03_03)
-
Highlight a Region in a Country with leaflet (03_04)
-
Compare Locations/Events with Geobubble Charts in leaflet (03_05)
-
Make a Choropleth Map of Discrete Variables with leaflet (03_06)
-
Make a Choropleth Map of Continuous Variables with leaflet (03_07)
-
Visualize Raster Data with leaflet (03_08)
-
Wrapping UpYou Did It!
Lesson 3 of 34
In Progress
mapview for Quick Maps (01_03)
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 must be logged in to post a comment.
What about tmap?
Hi Perry! In this course I chose to cover only one option for static and interactive maps. I prefer {ggplot2} because it’s more widely used than {tmap}, and it also has an extensive collection of packages that extend its capabilities. Here are some useful extensions when it comes to maps:
On top of this, the {cowplot} and {patchwork} packages are both designed for assembling multiple {ggplot2} charts together – which includes maps built using geom_sf().
However, I do recognise that {tmap} is very powerful and can be used to create beautiful maps. Because it is designed specifically with mapping in mind it often takes less code to do things in {tmap} vs {ggplot2}.
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.
Hi Irantzu, sorry this is happening. Could you tell me a little bit about your set up:
Example R version output
> version
_
platform x86_64-apple-darwin17.0
arch x86_64
os darwin17.0
system x86_64, darwin17.0
status
major 4
minor 1.1
year 2021
month 08
day 10
svn rev 80725
language R
version.string R version 4.1.1 (2021-08-10)
nickname Kick Things
Hi Charlie,
Thank you very much for your reply. Here ther versions:
– OS: Windows 10.
– R version:
> version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 4
minor 0.2
year 2020
month 06
day 22
svn rev 78730
language R
version.string R version 4.0.2 (2020-06-22)
nickname Taking Off Again
– RStudio version: Version 1.3.1073
Thank you,
irantzu
Thanks, Irantzu.
Your RStudio is fairly old, and I know that has caused issues for some packages like {mapview}. I’d recommend installing the latest version of RStudio and seeing if that fixes it.
If the problem remains the next thing I’d try is updating R. However, you’ll need to reinstall your packages afterwards.
If you continue to have the issue after upgrading both R and RStudio it’s likely there’s a bug and we should try and capture info about your system to help the package developer fix the bug. Could you comment again if you continue to have this issue? Thanks, Charlie
Thank you Charlie,
I have updated my R and Rstudio versions and now mapview is working.
Thanks, irantzu
Glad it’s working – hope you enjoy the rest of the course!
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!!.
Hello Latif. I’m sorry I didn’t quite understand your question, could you help me? Is the problem that the mapview maps are not showing at all for you? Are the maps showing in a separate window or your browser instead? Thanks, Charlotte.
Sorry for the mix up. I meant to say. “All the map effects below, do *not* show up in viewer pane of my RStudio.”
*The maps do show up*. However, my visuals differ from those in the lesson clip. For instance, the blue colored map of countries at 2:56 and 4:00 do not appear. At 4:12, a pop-up menu appear in the lesson video upon clicking (on Antarctica). I can’t get the menu to show.
Also, I do not see the rings around tiny countries at 4:56 and the colored map of states with the US at 5:32.
I hope my question is clearer now.🙏🏾
Hi Latif – thanks for coming back with more information. I wonder if you’ve got an older version of RStudio and that’s the issue. Could you try to update RStudio and see if {mapview} continues to not show the data layers?
For some reason when I hit play on this video, it doesn’t load. Any idea why? Thanks!
Nevermind. I restarted my computer and it seems to be working now.
I’m glad it’s working now!
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?
It appears that the package has been archived. We’re looking into this and will get back to you!
Hello Laura,
Sorry you’re having an issue with this. My understanding of the archive process is that it shouldn’t affect how remotes::install_github() works.
Could you reply with the output you get from running this code, please?
R.version
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!!
Hello Jeremy,
The good news is that you’re not making any mistakes. But, unfortunately the bottom is falling out of {raster} due to the retirement of {rgdal} in 2023. This means there will be unexpected behaviours as progress forwards in time. I need to think about how to update this course around those issues, as they’re coming in more quickly than I expected.
My recommended workflow is to move to {terra} which I demonstrate in this 4min video. Which using this code:
library(rnaturalearthdata)
# remotes::install_github("hrbrmstr/albersusa")
library(albersusa)
library(sf)
library(raster)
library(mapview)
alaska_landcover <- raster("data/alaska_landcover.img") alaska_landcover %>%
mapview()
# terra -------------------------------------------------------------------
library(terra)
alaska_terra <- rast("data/alaska_landcover.img") alaska_terra %>%
mapview()
# tidyterra ---------------------------------------------------------------
library(tidyverse)
library(tidyterra)
alaska_recoded <- alaska_terra %>%
mutate(red_recoded = case_when(Red == 75 ~ NA,
TRUE ~ Red))
ggplot() +
geom_spatraster(data = alaska_recoded,
aes(fill = red_recoded))
Wow!! thanks Charlie — this worked perfectly for me, and now I can see the map using geom_spatraster in ggplot — cool!!
thank you 😀
Hello Charlie,
Thanks for the new code. I tried it side and still got an error.
Any idea on this one? Thanks!!
Error in `dplyr::mutate()`:
ℹ In argument: `red_recoded = case_when(Red == 75 ~ NA, TRUE ~ Red)`.
Caused by error in `case_when()`:
! Failed to evaluate the left-hand side of formula 1.
Caused by error:
! object ‘Red’ not found
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
Error in `dplyr::mutate()`:
ℹ In argument: `red_recoded = case_when(Red == 75 ~ NA, TRUE ~ Red)`.
Caused by error in `case_when()`:
! Failed to evaluate the left-hand side of formula 1.
Caused by error:
! object ‘Red’ not found
—
Backtrace:
▆
1. ├─alaska_terra %>% …
2. ├─dplyr::mutate(…)
3. ├─tidyterra:::mutate.SpatRaster(…)
4. │ ├─dplyr::mutate(values, …)
5. │ └─dplyr:::mutate.data.frame(values, …)
6. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(…), by)
7. │ ├─base::withCallingHandlers(…)
8. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
9. │ └─mask$eval_all_mutate(quo)
10. │ └─dplyr (local) eval()
11. └─dplyr::case_when(Red == 75 ~ NA, TRUE ~ Red)
12. └─dplyr:::case_formula_evaluate(…)
13. ├─base::withCallingHandlers(…)
14. └─rlang::eval_tidy(pair$lhs, env = default_env)
Hello Elisee!
The error message is telling us that mutate wasn’t able to find a column called `Red` which suggests to me that you accidentally mis-selected or otherwise didn’t quite run the code in the correct order.
Can you check if you piped the dataset into mutate, eg
alaska_terra %>%
mutate(red_recoded = case_when(Red == 75 ~ NA,
TRUE ~ Red))
Thanks,
Charlie