Alina Lerner
Alina Lerner

Reputation: 59

Adding the coastline over plot in R

I need to make a map where the interpolated data is on it. My code for plotting is:

library(ggplot2)
theme_set(theme_bw())
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggthemes)
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
ggplot(data = world)+
  geom_tile(data = d, aes(x = lon, y = lat, fill=x))+
  geom_raster(data = d, aes(x = lon, y = lat, fill=x))+
  geom_sf()+
  coord_sf(xlim=c(3,35), ylim=c(52,72))

I have received a plot like this

enter image description here

But I need only the countries contour to be on the plot with no difference either it is land or sea. Could ypu please help me in terms of this boarders.

Upvotes: 2

Views: 1955

Answers (2)

P. Denelle
P. Denelle

Reputation: 830

You may use the function ne_coastline() if you are interested in plotting the coastlines only.

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

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

ggplot(world) +
  geom_sf() +
  coord_sf(xlim = c(3, 35), ylim = c(52, 72)) +
  theme_void()

enter image description here

Upvotes: 1

M--
M--

Reputation: 28825

You can set the set the alpha for fill in your geom_sf, so it won't be visible. Look below;

library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggthemes)
library(rgeos)

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

ggplot() +
  geom_tile(data = d, aes(x = lon, y = lat, fill=x)) +
  geom_raster(data = d, aes(x = lon, y = lat, fill=x)) +
  geom_sf(data = world, fill=alpha("lightgrey", 0), color="lightgrey") +
  coord_sf(xlim=c(3,35), ylim=c(52,72)) + 
  theme_bw()

Here's an example only showing the world map.

ggplot() +
  geom_sf(data = world, fill=alpha("lightgrey", 0), color="lightgrey") +
  coord_sf(xlim=c(3,35), ylim=c(52,72)) + 
  theme_bw()

Upvotes: 0

Related Questions