【问题标题】:Plot bathymetry and coastline using ggplot2 and marmap使用 ggplot2 和 marmap 绘制水深和海岸线
【发布时间】:2018-05-04 15:26:21
【问题描述】:

我环顾四周,并没有找到一个很好的解决方案来实现我的目标。 我想使用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 上的分布,土地为黑色加上灰色线用于测深。

【问题讨论】:

    标签: r ggplot2 maps


    【解决方案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") 
    

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

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

    【讨论】:

      【解决方案2】:

      这是一种方法:

      获取深海数据:

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

      【讨论】:

      • 这种方式确实不错。我用更多信息编辑了我的问题。
      • @Luca Stirnimann 我建议提出另一个问题,而不是编辑现有的、已经回答的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多