3

I am trying to plot these countries from a dataframe called data:

               country value        lon        lat
1              Denmark    12   9.501785  56.263920
2     UK:Great Britain    13  -1.174320  52.355518
3               France    15   2.213749  46.227638
4              Germany    17  10.451526  51.165691
5      China:Hong Kong    18 114.174695  22.278315
6          Netherlands    31   5.291266  52.132633
7          New Zealand    32 174.885971 -40.900557
8  UK:Northern Ireland    33  -6.492314  54.787715
9               Norway    34   8.468946  60.472024
10        Saudi Arabia    40  45.079162  23.885942
11              Serbia    41  21.005859  44.016521
12           Singapore    42 103.819836   1.352083
13     Slovak Republic    43 101.724578   3.153870
14            Slovenia    44  14.995463  46.151241
15        South Africa    45  22.937506 -30.559482

I am using the worldmap and ggplot libraries:

library(maps)       # Provides functions that let us plot the maps
library(ggplot2)    # Generic graphis engine

map = map_data("world")
map = subset(map, region!="Antarctica") #Remove Antarctica from map

Countries = ggplot() + 
  geom_polygon(data = map, aes(x=long, y = lat, group = group), fill = NA, colour="darkgray", size=0.5)+
  geom_map(data=data,map=map,aes(map_id=country, x=lon, y=lat),fill = "cornflowerblue", colour = "gray") +
  coord_equal()
Countries

I can plot all the countries except UK:Great Britain, China: Hong Kong and actually all other which region and subregion are separated by ":":

Country map

I run out of ideas on how to plot UK:Great Britain using world_map and ggplot. Have any of you have a similar problem, or can think of a solution? Thanks in advance.

JPMD
  • 644
  • 1
  • 7
  • 19
  • Stupid solution (but works): Save the mapdata to a txt file with `map = map_data("world") map = subset(map, region!="Antarctica") write.table(map, "C:/temp/map.txt", sep=";")` Go to excel and concatenate the region and subregion columns for which subregion is not NA and import back the csv saved file bacl to R `map <- read.csv("c:/Temp/map.csv", sep=";")` – JPMD Sep 01 '16 at 19:02
  • why do the concat work outside R @JPMD? And by "works" what do you mean? – hrbrmstr Sep 01 '16 at 19:07
  • I mean, I got the countries I wanted plotted. I am more used to Excel... I am still a newbie with R... – JPMD Sep 01 '16 at 20:47

2 Answers2

3

This achieves the result you are looking for, but I'm not sure it's a very useful map

library(ggplot2)
library(data.table)
library(magrittr)

map_dat <- subset(map_data("world"), region!="Antarctica")
setDT(map_dat)

# the countries you need to translate to region:subregion
colon_countries <-
  grep(':', data$country, value=T) %>%
    sub(':.*$', '', .) %>%
    unique

# change region to region:subregion, 
# for countries of interest, for rows with a value of subregion
map_dat[region %in% colon_countries, 
        region := ifelse(!is.na(subregion),
                         paste0(region, ':', subregion),
                         region)]
ggplot() + 
  geom_polygon(data = map_dat,
               aes(x=long, y = lat, group = group),
               fill = NA, colour="darkgray", size=0.5)+
  geom_map(data = data, map = map_dat,
           aes(map_id = country),
           fill = "cornflowerblue", colour = 'gray') +
  # probably not the projection you really want, but leaving it to match your post above
  coord_equal()

enter image description here

arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • Thanks a lot @arvi1000. I was not familiar with the ´library(data.table)´ and ´library(magrittr)´. What do you mean by not being the "projection you really want"... Are there more suitable or appropriate projections for world_maps? (mercator?). Cheers. – JPMD Sep 01 '16 at 20:54
  • data.table is just there to make it more convenient to perform the concatenation of region and subregion for a subset of rows; magrittr lets you use the pipe operator (`%>%`), which makes nesting functions more readable (`grep() %>% sub() %>% unique` is the same as `unique(sub(grep()))`) – arvi1000 Sep 01 '16 at 21:08
  • map projections are a matter of debate and personal preference (http://xkcd.com/977/). `coord_map()` let's you pick from a number of common options (including mercator by default), but I see that you get some weird artifacts with this dataset that way, due to polygons spanning the int'l date line. experiment and see what works for you! – arvi1000 Sep 01 '16 at 21:16
0

geom_map will match the entries in data$country to map$region. Unfortunately map_data splits regions by the first colon, so you get "UK" which doesn't match "UK:Great Britain" in data$country.

A possible manual fix is to correct like this:

map$region[which(map$subregion == "Great Britain")] <- "UK:Great Britain"
map$region[which(map$subregion == "Northern Ireland")] <- "UK:Northern Ireland"
map$region[which(map$subregion == "Hong Kong")] <- "China:Hong Kong"
Alex Deckmyn
  • 1,017
  • 6
  • 11