使用ggplot2和marmap绘制测深图和海岸线 [英] Plot bathymetry and coastline using ggplot2 and marmap

查看:51
本文介绍了使用ggplot2和marmap绘制测深图和海岸线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,但没有找到适合我目标的解决方案.我想使用 ggplot2 在经度/纬度图上绘制一些数据,并使用 marmap 在海岸线和测深图上绘制所有数据,

I looked around and I have not found a nice solution for my goal. I want to plot some data on a longitude/ latitude plot using ggplot2 and the coastline plus bathymetry with marmap, everything in one single plot.

此脚本用于绘制mydata

This script is to plot mydata

ggplot(data = ctd, aes(x = Longitude, y = Latitude)) +
  geom_raster(aes(fill = Temp)) +
  scale_fill_gradientn(colours = rev(my_colours)) +
  geom_contour(aes(z = Temp), binwidth = 2, colour = "black", alpha = 0.2) +

  #plot stations locations
  geom_point(data = ctd, aes(x = Longitude, y = Latitude),
             colour = 'black', size = 3, alpha = 1, shape = 15) +

  #plot legends
      labs(y = "Latitude", x = "Longitude", fill = "Temp (°C)") +
      coord_cartesian(expand = 0)+
      ggtitle("Temperature distribution") 

使用 marmap 下载测深仪

library(marmap)
Bathy <- getNOAA.bathy(lon1 = 37, lon2 = 38.7,
                       lat1 = -45.5, lat2 = -47.3, resolution = 1)

我想要获得的结果是mydata在Lon/Lat上的分布,用黑色加灰色的线表示地形的水深.

The result I would like to obtain is the distribution of mydata on Lon/Lat with the land colored in black plus grey lines for the bathymetry.

推荐答案

以下是一种方法:

获取大量数据:

library(marmap)
Bathy <- getNOAA.bathy(lon1 = 37, lon2 = 38.7,
                       lat1 = -45.5, lat2 = -47.3, resolution = 1)

将其转换为矩阵:

Bathy <- as.matrix(Bathy)
class(Bathy) <- "matrix"

现在将其重塑为长格式并绘图

now reshape it to long format and plot

library(tidyverse)

Bathy %>%
  as.data.frame() %>%
  rownames_to_column(var = "lon") %>%
  gather(lat, value, -1) %>%
  mutate_all(funs(as.numeric)) %>%
  ggplot()+
  geom_contour(aes(x = lon, y = lat, z = value), bins = 10, colour = "black") +
  coord_map()

这篇关于使用ggplot2和marmap绘制测深图和海岸线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆