2

I just had a play with ggplot2 in R, with syntax like

geom_map(data=world, map=world
aes(x=long, y=lat, map_id=region),
color="white", fill="#7f7f7f", size=0.05, alpha=1/4)

That gives me the world map, is it possible to only get UK map as background map? Many thanks Peddie

3
  • 1
    Syntax looks flawed. Missing comma or parenthesis. Edit question.
    – IRTFM
    Jan 2, 2016 at 22:20
  • 1
    Sorry I only copied and pasted part of the script. I hope to get advice on data and map, I saw a lot of samples like data=world, map=world, can I replace world with specific countries e.g UK? I did try UK but that throws error.
    – PeddiePooh
    Jan 2, 2016 at 22:41
  • Have you tried subsetting the data as in map_uk <- map_data('world') %>% filter(region == 'UK') ?
    – steveb
    Jan 2, 2016 at 22:45

4 Answers 4

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

8

You can also do something like:

library(ggplot2)
library(ggthemes)
library(raster)
library(rgeos)

gbr <- getData("GADM", country="GBR", level=0)
gbr <- gSimplify(gbr, 0.01)

gbr_map <- fortify(gbr)

gg <- ggplot()
gg <- gg + geom_map(map=gbr_map, data=gbr_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="#7f7f7f")
gg <- gg + coord_map()
gg <- gg + theme_map()
gg

Once my ggalt package is in CRAN you can even use a decent projection.

enter image description here

4
  • Awesome! One more thing please - is it possible to actually have uk map zoom in particular area eg south east England or by defined lat / long? Many thanks
    – PeddiePooh
    Jan 3, 2016 at 15:18
  • Try changing the coord_map() call to gg <- gg + coord_map(xlim=c(-4, 2), ylim=c(50, 53))
    – hrbrmstr
    Jan 3, 2016 at 21:08
  • wow that really works too you are a genius! one last question - how do you work out xlim, ylim?
    – PeddiePooh
    Jan 4, 2016 at 20:22
  • 2
    by looking at a map with labeled graticules (i.e. lat/lon lines with #'s)
    – hrbrmstr
    Jan 4, 2016 at 20:22
3

Also check out ggplot2::borders()

library(ggplot2)    
ggplot() + 
  borders(reg="UK", color="white", fill="#7f7f7f", alpha=1/4)

The result should be equal to the one from @eipi10.

2
library(maps)
library(ggplot2)

dat=map_data("world")

ggplot() + 
  geom_map(data=dat[dat$region=="UK",], map=dat[dat$region=="UK",],
         aes(x=long, y=lat, map_id=region),
         color="white", fill="#7f7f7f", size=0.05, alpha=1/4) +
  coord_fixed()

enter image description here

0
1

The library ggmap works very well with ggplot2.

You could try this:

library(ggmap)

#Google API
register_google(key = "your_api_key")

map <- get_map(location = 'UK', zoom = 6)
ggmap(map)

and add geom as you like.

4
  • ggmap is now (summer 2020) effectively defunct. Jun 22, 2020 at 21:34
  • @gregmacfarlane, what do you mean defunct? My code is still working. You just need to have a Google API and run this line register_google(key = "your_api_key") before mine. So I don't understand the downvote.
    – MLavoie
    Jun 23, 2020 at 10:31
  • I suppose it's philosophical; using Google Maps as the background layer in open research is a tricky philosophical issue, and the OpenStreetMap functionality has been eliminated. github.com/dkahle/ggmap/issues/117 Jun 24, 2020 at 21:20
  • I might suggest an edit to specify that the google API is now required. Jun 24, 2020 at 21:21

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.