Skip to content
R for the Rest of Us Logo

Mapping with R

34 lessons

Labelling ggplot2 Maps (02_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.

Your Turn

Open the 02_03 project. Use the your-turn.R script in 02_03 to create a labelled geoscatter plot.

Select a country of your choice and use that to create a labelled geoscatter plot of the 10 largest cities in the country.

The solution shows cities in Germany but the code will work for any region that exists in the world.cities dataset.

Learn More

The {ggrepel} package is really a delight to work with and it works well with many geoms built into {ggplot2}. As mentioned in the video, it doesn’t yet natively support sf objects. There’s a GitHub Issue requesting the feature. You can check that to see if you can skip the data manipulation trick I demoed in this video.

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

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

David Solet

David Solet

March 31, 2022

With current events in mind, I chose the 10 largest cities in Ukraine instead of Germany. It worked well for the lesson. In world.cities, the label for "Kyiv" was "Kiev", which I think is the old anglicized spelling. Is there a source for up-to-date country (and other geographic features) labels? Thanks!

Bohdanna Kinasevych

Bohdanna Kinasevych

April 7, 2022

Hi Charlie, I'm struggling to add labels to my map. I'm trying to plot 5 locations in Manitoba, Canada and it works up to the point when I convert my sf data file to a tibble and try to add labels. The X Y columns have the correct values for long/lat but my labels all plot at 0,0 (seems as though it's reading the X Y coordinates on a different scale). Any suggestions?

Here's my attempt to reproduce the code: manitoba_sf % st_as_sf() %>% filter(NAME_1 == "Manitoba")

mhrn_sites_tib <- tribble( ~location, ~long, ~lat, "Flin Flon", -101.88820, 54.76882, "Selkirk", -101.24217, 53.83253, "The Pas", -97.88055,55.75860, "Thompson", -97.29231, 49.85396, "Winnipeg", -97.29231, 49.85396, )

mhrn_sites_sf % st_as_sf(coords = c("long", "lat"), crs = 4326)

ggplot() + annotation_map_tile() + geom_sf(data = mhrn_sites_sf) + geom_label_repel(data = mhrn_sites_tib, aes(x = long, y = lat, label = location))

steve wakelin

steve wakelin

April 21, 2022

Hi Charlie I couldn't seem to get this working very well for New Zealand. The cities plotted on an x y plot (with gridlines for the long and lat - using theme_minimal(), but there was no actual country outline for New Zealand.

Enjoying the course!
Kind regards Steve

gabrielle van Son

gabrielle van Son

January 14, 2024

Hi Charlie, I tried the Netherlands. But now my shapefile is very, very tiny in the upperright corner of a map that also includes parts of Africa. I need to zoom in somehow. Do you have some advice? Thank you! Best, Gabrielle