1

There don't appear to be any examples in the documentation for the maps package or on stack overflow of anyone trying to plot text or other information in place of a country name, rather than regions within a country.

library(maps)

test <- structure(list(countries = structure(c(3L, 4L, 2L, 1L), .Label = c("France", 
"Germany", "Italy", "UK"), class = "factor"), value = c(20, 13, 
42, 6)), .Names = c("countries", "value"), row.names = c(NA, 
-4L), class = "data.frame")

map.text("world", c("France", "Germany", "Italy", "UK"))

Map with all regions labelled, rather than just the country names

I plan on replacing the country names with the values. The solution might look something like

map.text("world",test$countries, labels=test$values)

but I can't figure out how to get it to leave out all the unnecessary region labels.

Henry E
  • 350
  • 4
  • 20

1 Answers1

2

map.text tries to match regions with regex, causing the problem. You can either use exact = TRUE to disable this, or take advantage of it by putting a $ (end of line) after each. The UK is not strictly speaking a country, though, so you need to select the component parts, or otherwise work around it.

All told,

map.text("world", region = c("France$", "Germany$", "Italy$", 
                            "UK:Great Britain", "UK:Northern Ireland"))

returns plot with 5 names which is better, if not exactly perfect. Setting the labels lets us make it look like we want:

map.text("world", region = c("France$", "Germany$", "Italy$", 
                             "UK:Great Britain", "UK:Northern Ireland"), 
         labels = c("France", "Germany", '', "UK", "Italy"))

plot with four names Careful, order in the labels argument is finicky.

If you reconstruct test as such:

test <- data.frame(countries = c("Germany$", "France$", "UK:Northern Ireland", 
                                 "UK:Great Britain", "Italy$"),
                   value = c(42, 6, '', 13, 20), stringsAsFactors = FALSE)

so it looks like

> test
            countries value
1            Germany$    42
2             France$     6
3 UK:Northern Ireland      
4    UK:Great Britain    13
5              Italy$    20

you can plot the value column as the label:

map.text("world", region = test$countries, labels = test$value)

plot with number labels

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • This definitely works but you're right, there's a serious issue with labels and regions not being applied in the same order. There's [another answer](http://stackoverflow.com/a/30305806/4507677) I found that explains how to calculate the centre of a region's polygon for the purposes of graphing in ggplot. Now that I can pull out just the relevant regions I'm sorted. Thanks very much. – Henry E Jan 26 '16 at 09:28
  • You're right, the `labels` argument is really sensitive to order, and I'm really not sure what order it wants (a bug, I think). If you're doing this for a really big map with hundreds of labels, this approach may be impractical. – alistaire Jan 26 '16 at 15:18