地热编码的地震热图

时间:2017-06-20 20:35:28

标签: r ggplot2 plotly heatmap ggmap

我想在吉尔罗伊加利福尼亚州绘制地震加速度的热度。以下是我的数据;每个点的地理编码都是十进制的。

    lon          lat            Acc.

-121.5897466    36.98443922 0.308722537
-121.5776472    36.98446622 0.343560598
-121.5657399    36.98449289 0.316238725
-121.5528172    36.98452208 0.289579654
-121.5397651    36.98455121 0.278277577
-121.6022388    36.9957913  0.321904187
-121.5897466    36.99578578 0.346187454
-121.57767    36.99578046   0.323865427
-121.5657514    36.99577518 0.296775313
-121.5528643    36.99576944 0.281054324
-121.5398582    36.99576372 0.270957516
-121.6264404    37.00268339 0.3504049
-121.614493     37.00268494 0.343426824
-121.6022388    37.00268646 0.34803746
-121.5897466    37.00268806 0.316975267
-121.5776805    37.00268967 0.300399358
-121.5657618    37.00269118 0.290519468
-121.5528861    37.0026927  0.27529488
-121.5399123    37.00269441 0.264715439
-121.6264404    37.01301756 0.352218819
-121.614493     37.01301354 0.342255779
-121.6022388    37.01300933 0.333444018
-121.5897466    37.01300512 0.315921346
-121.5777013    37.01300115 0.302762624
-121.5657723    37.01299709 0.291672835
-121.5529293    37.01299261 0.266912813
-121.614493     37.0204378  0.327864875
-121.6022388    37.0204297  0.321358226
-121.5897466    37.0204215  0.305797414
-121.5777125    37.02041366 0.293992548
-121.5657835    37.0204058  0.283948148
-121.614493    37.0299123   0.313950088
-121.6022388    37.02991694 0.303625182
-121.5897466    37.02992166 0.291511686
-121.5777299    37.02992617 0.282628812
-121.5657949    37.02993068 0.271427682

以下是我的代码:

    GilroyMap=   qmap(location = c(lon = -121.5837188, lat = 37.007846),zoom=12,color="color",legend="topleft",maptype = "terrain", darken=0.0, extent = "device")
data$C <- cut(data$Acc., breaks=10)

    Q=GilroyMap+stat_density2d(data = data, aes(x = lon, y = lat,fill = Acc. ), colour = NA, alpha = 0.5) +

        scale_fill_distiller(palette =1 , breaks = pretty_breaks(n = 10)) +
        labs(fill = "") +
        theme_nothing(legend = TRUE) +guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)))

它不起作用!

2 个答案:

答案 0 :(得分:2)

您可能希望使用ggplotggmap个包:

library(ggmap)
library(ggplot2)


gilroy <- get_map(location = 'gilroy', zoom =12) 
ggmap(gilroy)


rbPal <- colorRampPalette(c('blue','red'))
mydata$Col <- rbPal(10)[as.numeric(cut(mydata$Acc.,breaks = 10))]

ggmap(gilroy, extent = "device") + geom_point(aes(x = lon, y = lat), colour = mydata$Col, 
      alpha = mydata$Acc., size = 6, shape = 15, data = mydata)

这将给我们:

enter image description here

您也可以使用plotly包参考Maps in R;

执行此操作
library(plotly)

g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showland = TRUE,
  landcolor = toRGB("gray95"),
  subunitcolor = toRGB("gray85"),
  countrycolor = toRGB("gray85"),
  countrywidth = 0.5,
  subunitwidth = 0.5)


p <- plot_geo(mydata, lat = ~lat, lon = ~lon) %>%
  add_markers( text = ~paste(lat, lon, Acc.,sep = "<br />"),
    color = ~Acc., symbol = I("square"), size = I(8), hoverinfo = "Acc."
  ) %>%
  colorbar(title = "Acc.") %>%
  layout(
    title = 'California Earthquake', geo = g)

<强> 数据:

@ 42-:“然而,更好的做法是将所有数据实例更改为不是R函数名称的名称”(即mydata)。

read.table(text='lon          lat            Acc.

        -121.5897466    36.98443922 0.308722537
        -121.5776472    36.98446622 0.343560598
        -121.5657399    36.98449289 0.316238725
        -121.5528172    36.98452208 0.289579654
        -121.5397651    36.98455121 0.278277577
        -121.6022388    36.9957913  0.321904187
        -121.5897466    36.99578578 0.346187454
        -121.57767    36.99578046   0.323865427
        -121.5657514    36.99577518 0.296775313
        -121.5528643    36.99576944 0.281054324
        -121.5398582    36.99576372 0.270957516
        -121.6264404    37.00268339 0.3504049
        -121.614493     37.00268494 0.343426824
        -121.6022388    37.00268646 0.34803746
        -121.5897466    37.00268806 0.316975267
        -121.5776805    37.00268967 0.300399358
        -121.5657618    37.00269118 0.290519468
        -121.5528861    37.0026927  0.27529488
        -121.5399123    37.00269441 0.264715439
        -121.6264404    37.01301756 0.352218819
        -121.614493     37.01301354 0.342255779
        -121.6022388    37.01300933 0.333444018
        -121.5897466    37.01300512 0.315921346
        -121.5777013    37.01300115 0.302762624
        -121.5657723    37.01299709 0.291672835
        -121.5529293    37.01299261 0.266912813
        -121.614493     37.0204378  0.327864875
        -121.6022388    37.0204297  0.321358226
        -121.5897466    37.0204215  0.305797414
        -121.5777125    37.02041366 0.293992548
        -121.5657835    37.0204058  0.283948148
        -121.614493    37.0299123   0.313950088
        -121.6022388    37.02991694 0.303625182
        -121.5897466    37.02992166 0.291511686
        -121.5777299    37.02992617 0.282628812
        -121.5657949    37.02993068 0.271427682', header=TRUE, quote='"') -> 
    mydata  #named to have both code sections work and avoid a function name.

答案 1 :(得分:2)

使用Google地图的解决方案如何(使用它需要api key)?

library(googleway)

mapKey <- 'your_api_key'

google_map(key = mapKey) %>%
    add_heatmap(data = df, weight = "Acc.", option_radius = 0.02)

enter image description here