Search code examples
rgeolocation

R latitude longitude visualization


I'm trying to make a visualization of latitude and longitude values. Thanks to another post I managed to get the right values to be passed to ggplot. The code I'm using is as follows:

tweets <- searchTwitter('weather', n=1000,lang='en')
t <- twListToDF(tweets)
lat <- t[, c("latitude")]
lon <- t[, c("longitude")]
l.df <- data.frame(lat,lon)
l.df <- na.omit(l.df)
require(ggplot2)
ggplot(l.df, aes(x=l.df$lon, y=l.df$lat,)) + geom_point(size=10.9, alpha=.02)

The problem is that I'm getting a nearly empty plot. I'm saying nearly empty as there are some spots but are difficult to identify

How can I get a plot to display the map of the world and dots for the geo-location?

enter image description here


Solution

  • With the leaflet package, you can simply call the map with your data.frame and pass in the values of latitude and longitude to create markers and get started:

    library(leaflet)
    yourMap <- leaflet(l.df) %>% addTiles() %>% addCircles(lng = ~lon, lat = ~lat)
    yourMap      # Prints the map
    

    If you choose to use leaflet, be sure to install dependencies because the %>% is the piping operator from the magrittr package.

    I suggested leaflet since, if you're looking to share your data and/or ever host it on a Shiny web server, they supply great documentation on the RStudio "Leaflet for R" Github page for its use, as I mentioned in the comments.