【问题标题】:Add contour on ggmap在ggmap上添加轮廓
【发布时间】:2017-09-12 00:09:41
【问题描述】:

首先,这是我的数据集。

Lon     Lat CPUE    Temperature
120.93  27  0.00    24.3
121.18  27  0.62    24.2
121.43  27  3.76    24.9
121.6   27.25   0.87    25
121.35  27.25   1.63    24.2
121.1   27.25   2.66    24.8
121.25  27.5    7.37    24.9
121.5   27.5    6.26    25.2
121.75  27.5    12.02   19.4
121.95  27.75   30.40   18.5
121.7   27.75   93.81   23.1
121.65  28  282.83  27.1
121.9   28  10.43   22.3
122.15  28  36.11   18.2
122.4   28.25   170.05  17.9
122.15  28.25   1170.97 18.8
122.3   28.5    0.00    18.4
122.55  28.5    149.99  17.6
122.8   28.75   118.27  18.5
122.55  28.75   1838.31 17.6
122.25  29  1218.93 21.2
122.5   29  1245.63 18.7
122.75  29  235.07  17.9 123    29  33.01   19.1

这是我的带有 CPUE 数据点的 ggmap:

我想知道我是否可以在这个 ggmap 上添加温度等高线。

【问题讨论】:

    标签: r ggplot2 ggmap


    【解决方案1】:

    这是一种方法,参考this answer,因为数据框中没有足够的数据点来生成轮廓数据:

    # generate regular grid since geom_contour doesn't work on this dataset
    temp <- akima::interp(df$Lon, df$Lat, df$Temperature)
    df.expanded <- expand.grid(x = temp$x, y = temp$y)
    df.expanded$z <- as.vector(temp$z)
    df.expanded <- na.omit(df.expanded)
    rm(temp)
    
    library(ggplot2)
    
    # plot contour using stat_contour() on expanded data frame
    ggmap(background) +
      stat_contour(data = df.expanded, binwidth = 1,
                   aes(x=x,y=y,z=z)) +
      geom_point(data = df, alpha = 0.5,
                 aes(x = Lon, y = Lat, size = CPUE))
    

    数据:

    df <- read.table(header = T, text = "Lon     Lat CPUE    Temperature
    120.93  27  0.00    24.3
                     121.18  27  0.62    24.2
                     121.43  27  3.76    24.9
                     121.6   27.25   0.87    25
                     121.35  27.25   1.63    24.2
                     121.1   27.25   2.66    24.8
                     121.25  27.5    7.37    24.9
                     121.5   27.5    6.26    25.2
                     121.75  27.5    12.02   19.4
                     121.95  27.75   30.40   18.5
                     121.7   27.75   93.81   23.1
                     121.65  28  282.83  27.1
                     121.9   28  10.43   22.3
                     122.15  28  36.11   18.2
                     122.4   28.25   170.05  17.9
                     122.15  28.25   1170.97 18.8
                     122.3   28.5    0.00    18.4
                     122.55  28.5    149.99  17.6
                     122.8   28.75   118.27  18.5
                     122.55  28.75   1838.31 17.6
                     122.25  29  1218.93 21.2
                     122.5   29  1245.63 18.7
                     122.75  29  235.07  17.9 123    29  33.01   19.1")
    

    背景图数据:

    library(ggmap)
    background <- get_map(location = c(min(df$Lon),
                                       min(df$Lat),
                                       max(df$Lon),
                                       max(df$Lat)),
                          zoom = 8)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2015-04-27
      相关资源
      最近更新 更多