0

I have a dataframe like the following:

country <- c('US', 'CA','TW')
city <- c('Atlanta','Halifax','Taipei')
lat <- c(33.8004, 44.6500, 25.0392 )
lon <- c( -84.3865,-63.6000,121.5250 )

data <- data_frame(country,city, lat, lon)

> data
# A tibble: 3 × 4
  country    city     lat      lon
    <chr>   <chr>   <dbl>    <dbl>
1      US Atlanta 33.8004 -84.3865
2      CA Halifax 44.6500 -63.6000
3      TW  Taipei 25.0392 121.5250

Think of this data as each observations is a mobile call from a city around the world (there could be multiple call from the same city of course).

There are two things I would like to do with this data.

  1. Simplest map. I would simply like to (nicely) plot these calls as a many dots on a global map. So dots may of course be overlaid (with some alpha blending)

  2. Nicer map. First, count how many observations per cities (easy to do with dplyr), and then create a map where polygons are cities. That way, each city has a color according to the number of calls in that city. Ideally I would like to get something like

enter image description here

but for cities on a global map.

Problem is: I dont know where to get the right maps for this and to complicate the matter I can only download the maps manually (not via get_map for instance)

Any ideas how to get these two simple maps?

  • 1
    If a world map (e.g. lat: 80 ~ -80, lon: -160 ~ 160) is downloaded, you can add annotation_raster(Downloaded_Image, xmin= -160, xmax= 160, ymin= 80, ymax= -80, interpolate = TRUE) to use it as backdrop in ggplot2. – Kazuhito Dec 14 '16 at 13:51
  • 1
    Sorry it was too short. I will expand and post it. Give me a sec. – Kazuhito Dec 14 '16 at 13:58
  • 1
    I take it you don't want to use openstreetmap.org maps? – barrycarter Dec 14 '16 at 17:43
  • 1
    So, download the OSM slippy tiles. – barrycarter Dec 14 '16 at 17:45
  • 1
    This comment thread (and maybe this question) will probably get closed/nuked/sent to a chat room because it's a little bit trivial (no offense to you intended, everyone has to start somewhere). It'd be easier for me to give advice in chat, either here, or google hangouts carter.barry@gmail.com – barrycarter Dec 14 '16 at 17:48
2

Sample code.

Given a world map (e.g. lat: 80 ~ -80, lon: -160 ~ 160) was downloaded as PNG ("Downloaded_Image.png").

library(png)
Downloaded_Image <- readPNG("Downloaded_Image.png")

library(ggplot2)
ggplot(data= data, aes(x= lon, y= lat)) +
  annotation_raster(Downloaded_Image, 
                xmin= -160, xmax= 160, ymin= 80, ymax= -80, 
                interpolate = TRUE) +
  geom_point() +
  scale_x_continuous(name= "", 
                 limits= c(-160, 160), 
                 breaks= seq(-160, 160, by= 20), 
                 expand = c(0, 0)) +
  scale_y_continuous(name= "",
                 limits= c(-80, 80), 
                 breaks= seq(-80, 80, by= 20), 
                 expand= c(0, 0)) +
  theme_bw()

If your image is plotted upside-down, please exchange ymin / ymax value.

As I have not tested this, please let me know if you find troubles.

  • 1
    I don't know. Any map would do, as far as you can find lat-lon coordinate of 4 corners. – Kazuhito Dec 14 '16 at 14:23
  • 1
    I have been trying to understand your question, but it does not fit well with your original question. When you posted it, what kind of map you had in your mind? And can you explain what location your Shapefile will represent? Like point Shapefile for cities? Please help me to be able to reply to you. – Kazuhito Dec 14 '16 at 14:42
  • 1
    @Noobie Thanks for updates on the post. Now I understand you need to create thematic maps. As you are familiar with ggmap, google maps would suit you. For your polygons, you need geom_polygon() to replace geom_point() above, following fortify(). So, YES, I agree with you that you need polygon Shapefile to define your city area. – Kazuhito Dec 14 '16 at 15:13
  • 1
    @Noobie I'm afraid not, with that data. You will have to find polygon Shapefile first. – Kazuhito Dec 14 '16 at 15:17
  • 1
    Umm, most maps are not equiangular, so the latitude formula given here wouldn't work. Try Google/OSM Maps and KML files for now and work your way up to what you want, perhaps? – barrycarter Dec 14 '16 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.