0

I have managed to create a map of Europe below which works.

  1. How can I add a dataset (which is in a .csv format) which has lat and lons. I made a dummy dataset below and I need to overlay the lat/lon for each Place and mark it with an x for metricA and o for metricB. Is there a way I can do this?

  2. The map comes up really small and the margins are really big. I tried to add theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) based on this example from stack overflow R ggplot, remove white margins in ggsave/ggplot but when I add it before coord_sf(xlim = c(-40, 60), ylim = c(30, 90)) + I get an Error

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found and when I add it after, it doesn't change the margins to make the plot bigger. What can I do to make the figure bigger and the margins smaller?

 library("ggplot2")
 theme_set(theme_bw())
 library("sf")
 library("rnaturalearth") 
 library("rnaturalearthdata")

 world <- ne_countries(scale = "medium", returnclass = "sf") 
 class(world)
             
ggplot(data = world) +
geom_sf(color = "black", fill = "grey") +  
coord_sf(xlim = c(-40, 60), ylim = c(30, 90)) +
ggtitle("Europe") +
xlab('Longitude') + ylab('Latitude'))

Dataset:

 Place  MetricA    MetricB   Lat   Lon
 A                   x       55    -40
 B      x                    60    -20
 C                   x       75     18
 D      x            x       68     3
 E                   x       35     18
 F                   x       74     42
 G      x                    62    -26
 H                   x       30    -30
 I      x            x       69     15
liv
  • 93
  • 10

2 Answers2

1

I believe I have an answer to half of your question. Essentially geom_sf behaves somewhat peculiarly relative to other ggplot2 geoms in that it enforces an aspect ratio for maps. I was somewhat frustrated by this at first, but it really makes sense when it comes to mapping. To change the margin size you just have to change the aspect ratio of the plot, either when you export it by adjusting the ratio of the height and width by entering different numbers or in an RStudio session by pulling around the plots pane.

As for your second question, I forget the specifics, but I know the answer is laid out clearly here https://www.r-spatial.org/r/2018/10/25/ggplot2-sf-2.html.

Have a good rest of your day, and good luck with your project.

Austin Graves
  • 1,044
  • 5
  • 12
  • Hi Omri, I geom_sf(size=0.25) and geom_sf(size-3) and it changes the size of the inside of the figure but not the overall map size. Do you recommend a different way to change the aspect ratio? Thank you! – liv Nov 18 '20 at 08:28
  • 1
    Changing the size of the plot will not fix the issue. In fact, I don't believe that doing anything with the ggplot itself will fix the issue. When you export with a command like pdf, png, etc. then you can change the height and width to be the correct ratio. Alternatively, you can drag the edge of the pane labeled "Plots" in RStudio to change the aspect ratio. The issue with the large margins is a matter of how the plot is viewed, not one with the plot itself. – Austin Graves Nov 18 '20 at 13:43
1

You can overlay your map with points like so:

  1. Convert your dataset to long format via e.g. tidyr::pivot_longer so that you metric variablea become categories of one variable
  2. Add the points to your map via geom_point where you map the new metric variable on `shape´
  3. Set the desired shapes via scale_shape_manual

Concerning your second question. I was not able to reproduce you error. Adjusting the plot margins worked fine. However, I'm not sure whether this will be sufficient to remove the margins when saving or plotting. As already pointed out by @OmriNewett, the issue is that maps are special in that the aspect ratio is fixed and to get rid of the margins you have to set the ratio of width and height so match the fixed aspect ratio.

library(tidyr)
library(dplyr)
library("ggplot2")
library("sf")
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library("rnaturalearth") 
library("rnaturalearthdata")

theme_set(theme_bw())

world <- ne_countries(scale = "medium", returnclass = "sf") 

d <- read.table(text = "Place  MetricA    MetricB   Lat   Lon
 A      NA             x       55    -40
 B      x           NA       60    -20
 C      NA           x       75     18
 D      x            x       68     3
 E      NA           x       35     18
 F      NA           x       74     42
 G      x           NA       62    -26
 H      NA           x       30    -30
 I      x            x       69     15", header = TRUE)

d <- d %>% 
  tidyr::pivot_longer(-c(Place, Lat, Lon), names_to = "Metric") %>% 
  filter(!is.na(value))

ggplot(data = world) +
  geom_sf(color = "black", fill = "grey") +
  geom_point(data = d, aes(x = Lon, y = Lat, shape = Metric)) +
  scale_shape_manual(values = c(MetricA = 4, MetricB = 1)) +
  theme(plot.margin=grid::unit(c(0,0,0,0), "mm")) +
  coord_sf(xlim = c(-40, 60), ylim = c(30, 90)) +
  ggtitle("Europe") +
  xlab('Longitude') + ylab('Latitude')

stefan
  • 90,330
  • 6
  • 25
  • 51
  • I tried your code but I get this error: Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found what am I doing wrong? Im using it exactly as it is. – liv Nov 18 '20 at 07:45
  • I found on this website that with this error I have to close and reopen R. After shutting it down and reopening R, it works but when I run it again I get the same error. Is there a way around this so I don't have to shut down R and reopen every time I want to make a map? https://community.rstudio.com/t/still-fighting-with-grid-call-c-textbounds-as-graphicsannot-x-label-x-x-x-y-polygon-edge-not-found-error/65559 – liv Nov 18 '20 at 07:53
  • Hi Liv. As I said, I can't reproduce this error. If restarting does not solve the issue you should try to update your packages as suggested in the post you added as a link. – stefan Nov 18 '20 at 09:27