首页 > 解决方案 > 在R中的绘图上添加海岸线

问题描述

我需要制作一张插值数据所在的地图。我的绘图代码是:

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))

我收到了这样的情节

在此处输入图像描述

但我只需要国家轮廓就可以在情节上,无论是陆地还是海洋,都没有区别。ypu 可以在这个寄宿生方面帮助我吗?

标签: rggplot2plotraster

解决方案


您可以在您的alpha中设置 set ,因此它不可见。往下看;fillgeom_sf

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()

这是一个仅显示世界地图的示例。

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


推荐阅读