使用ggplot2和marmap绘制测深和海岸线

时间:2018-05-01 15:49:59

标签: r ggplot2 maps

我环顾四周,并没有为我的目标找到一个很好的解决方案。 我想使用ggplot2绘制经度/纬度图上的一些数据,使用marmap绘制海岸线加测深图,一切都在一个图中。

此脚本用于绘制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)

我想得到的结果是在Lon / Lat上的mydata分布,其中陆地用黑色加上灰色线条用于测深。

2 个答案:

答案 0 :(得分:1)

这是一种方法:

获得沐浴数据:

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"

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

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

enter image description here

答案 1 :(得分:1)

嗯,有一个marmap功能。它被称为autoplot.bathy()。你检查了它的帮助文件吗?

library(marmap) ; library(ggplot2)

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

ctd <- data.frame(Longitude = c(37.5, 38, 38.5), Latitude = c(-47, -46.5, -46))

autoplot.bathy(Bathy, geom=c("tile","contour")) +
    scale_fill_gradient2(low="dodgerblue4", mid="gainsboro", high="darkgreen") +
    geom_point(data = ctd, aes(x = Longitude, y = Latitude),
               colour = 'black', size = 3, alpha = 1, shape = 15) +
    labs(y = "Latitude", x = "Longitude", fill = "Elevation") +
    coord_cartesian(expand = 0)+
    ggtitle("A marmap map with ggplot2") 

enter image description here

或者,使用基本图形(和正确的宽高比):

# Creating color palettes
blues <- c("lightsteelblue4", "lightsteelblue3", "lightsteelblue2", "lightsteelblue1")
greys <- c(grey(0.6), grey(0.93), grey(0.99))

# Plot
plot(Bathy, image = TRUE, land = TRUE, n=30, lwd = 0.1, bpal = list(c(0, max(Bathy), greys), c(min(Bathy), 0, blues)), drawlabels = TRUE)

# Add coastline
plot(Bathy, deep = 0, shallow = 0, step = 0, lwd=2, add = TRUE)

# Add stations
points(ctd, pch=15, cex=1.5)

enter image description here