3
library(raster)
library(ggplot2)

map.shp <- getData('GADM', country='FRA', level=1)

plot(map.shp)
ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()

How can I put the map.shp on the right side of the ggplot as a small inset.

0

1 Answer 1

6

I think there are multiple ways to achieve this but a simple way would be to use cowplot and ggdraw:

I am using sf along with geom_sf from the development version of ggplot2 available at devtools::install_github("hadley/ggplot2")

require(ggplot2)
require(cowplot)
require(sf)
require(raster)

map.shp <- getData('GADM', country='FRA', level=1) %>% st_as_sf()



plot.cars <- ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
plot.map <- ggplot() + geom_sf(data=map.shp)

inset_map <- ggdraw() +
  draw_plot(plot.cars, 0, 0, 1, 1)+
  draw_plot(plot.map, 0.5, 0.52, 0.5, 0.4) 

enter image description here

Sorry the shapefile seemed to be rendering really slow for me so couldn't play around with the positioning too much. Is something like this what you were after?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. This is what I was trying to achieve. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.