Felix Analytix

Felix Analytix

How to Use generative AI to Create World Maps in R

How to use the new Positron Assistant to generate interactive world maps with R

Felix Analytix's avatar
Felix Analytix
Sep 05, 2025
∙ Paid
Share

Would you like to learn how to create interactive world maps in R using generative AI?

Discover how to use the new Positron Assistant AI agent to automatically generate R code that produces interactive world maps on your own dataset.

In this tutorial, you will learn how to:

  • Use LLM prompts on your own data to generate R code with the Positron Assistant and the ellmer R package

  • Prepare and join your dataset with world geodata using rnaturalearth and countrycode

  • Design interactive choropleth world maps with the leaflet package

  • Add informative labels, detailed popups, and a polished legend to your maps

We will carefully review the R code generated by the LLM prompt, so you can easily adapt the code for your own use cases.

Check out the video now:

Setting Up Claude Sonnet 4

First, let’s see how to set up the ellmer package to chat with a large language model (LLM), in this case, Claude Sonnet 4. It’s also good practice to manage your API keys securely using your .Renviron file.

library(ellmer)

# get the API key: https://console.anthropic.com/settings/admin-keys
# add ANTHROPIC_API_KEY=xxx in .Renviron using usethis::edit_r_environ()
chat <- chat_anthropic(
  api_key = Sys.getenv("ANTHROPIC_API_KEY"),
  model = "claude-sonnet-4-20250514",
  params = params(temperature = 0) # reduce randomness
)

Here, we’re first loading the ellmer package which is our interface to the LLM. We create a chat object using our API key (securely stored in .Renviron). Setting temperature = 0 helps make the code generated by the LLM as reproducible as possible—this minimizes randomness.

Generating R Code to Fetch Country GDP Data

Now, we want the LLM to generate R code that fetches country-level GDP data for 2023 using the wbstats R package. The prompt is designed to return only the code, without code block syntax.

prompt_get_data <- "Get country gdp for 2023 using wbstats R package. Return only code without backsticks."

code_generated <- chat$chat(prompt_get_data)

chat$get_cost() # show costs
[1] $0.00

Here, we’ve put our prompt as a string and passed it to our chat object to generate the R code. We can also call get_cost() to check how much this interaction costs—it’s pretty cheap as less than $0.00.

Subscribe to receive by email FOR FREE the URL of the GitHub repository where you can access all the R code of this tutorial.

Fetching and Cleaning Data

Let’s now use the generated code to fetch the GDP data and prepare it for mapping. Here, we use the wbstats package to get GDP values, and the dplyr package to clean and organize our dataset to two essential columns: country name and GDP.

library(wbstats)

gdp_2023 <- wb_data(
  indicator = "NY.GDP.MKTP.CD",
  start_date = 2023,
  end_date = 2023
)

print(gdp_2023)
# A tibble: 217 × 9
   iso2c iso3c country             date NY.GDP.MKTP.CD unit  obs_status footnote
   <chr> <chr> <chr>              <dbl>          <dbl> <chr> <chr>      <chr>   
 1 AW    ABW   Aruba               2023    3648573136. <NA>  <NA>       <NA>    
 2 AF    AFG   Afghanistan         2023   17152234637. <NA>  <NA>       <NA>    
 3 AO    AGO   Angola              2023   84875162197. <NA>  <NA>       <NA>    
 4 AL    ALB   Albania             2023   23547180412. <NA>  <NA>       <NA>    
 5 AD    AND   Andorra             2023    3785067332. <NA>  <NA>       <NA>    
 6 AE    ARE   United Arab Emira…  2023  514130432648. <NA>  <NA>       <NA>    
 7 AR    ARG   Argentina           2023  646075277525. <NA>  <NA>       <NA>    
 8 AM    ARM   Armenia             2023   24085749592. <NA>  <NA>       <NA>    
 9 AS    ASM   American Samoa      2023            NA  <NA>  <NA>       <NA>    
10 AG    ATG   Antigua and Barbu…  2023    2005785185. <NA>  <NA>       <NA>    
# ℹ 207 more rows
# ℹ 1 more variable: last_updated <date>
library(dplyr)

gdp_2023_cleaned <- gdp_2023 |>
  rename(GDP = NY.GDP.MKTP.CD) |>
  select(country, GDP)

We use wb_data() to download the data: each row represents a country and its 2023 GDP. The rename and select steps ensure our data has a simple and clean structure: just country and GDP, making it ready for merging with geographic data.

Loading a System Prompt Template

The next step is to use a prompt template. In Positron, you can specify reusable markdown prompt templates (with .prompt.md extension) that provide detailed instructions to the LLM.

By reading and collapsing the prompt template, we have a reproducible way to instruct the LLM exactly how we want our world map code to be generated—encapsulating prompt engineering in one place.

Let’s load our prompt template into R now.

👇👇👇 Full prompt below 👇👇👇

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Felix Analytix
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture