Graphing IP Geolocation Data in R

dylan hudson
4 min readSep 24, 2021

This article has two sections: Retrieving IP geolocation data from within the R environment, and then graphing the results on a world map. The latter can be generalized to any latitude/longitude data, so if you’re just looking for info on world-map graphing, skip to Section 2.

Section 1: Geolocating IP Addresses from R
If you have a dataset with a list of IP address, you can use the ‘ip_api()’ function from the rgeolocate package to natively make the API calls from inside R and return the results into a data frame. I prefer this one over the flat-file Maxmind library option.

First, we need to install the ‘rgeolocate’ package- install.packages(‘rgeolocate')
require(rgeolocate)

You can now call ip_api() on IP addresses contained in a vector or column from a data frame. In this example, I have a data frame of IP addresses, and a corresponding ‘count’ column, like you might see if you were collecting information about website traffic.

So we’ll pass in the IP column and save the results in a data frame: geo_results <- ip_api(ip_data$IP, as_data_frame = TRUE, delay = FALSE)

The resulting data frame should look something like this-

Screenshot of dataframe returned by freegeoip function.

The data will be in the same order as the list you passed in, so to set ourselves up…

--

--