问题
I would like to plot the heat of earthquake acceleration at Gilroy California. The following is my data; geocodes of each point is in decimal.
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
The following is my code:
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)))
It does not work!
回答1:
How about a solution using Google Maps (to use it you need an api key)?
library(googleway)
mapKey <- 'your_api_key'
google_map(key = mapKey) %>%
add_heatmap(data = df, weight = "Acc.", option_radius = 0.02)
回答2:
You may want to use ggplot
and ggmap
packages:
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)
This will give us:
You can also do this using plotly
package in reference to 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)
Data:
@42-: "It would, however, be better practice to change all instances of data to a name that was not also an R-function name" (i.e. 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.
来源:https://stackoverflow.com/questions/44662532/heat-map-of-earthquake-by-geocodes