ggmap 具有价值的热图
ggmap Heatmap with value
谁能帮我制作 ggmap(热图)?
我的数据:
val_Qtd <- c(34, 10, 11, 7, 55, 18, 33, 16, 16, 249)
nom_State <- c("Distrito Federal","Bahia","Ceara","Espirito Santo","Minas Gerais","Parana","Rio de Janeiro","Rio Grande do Sul","Santa Catarina","Sao Paulo")
lon <- c(-47.86447, -41.70073, -39.32062, -40.30886, -44.55503, -52.02154, -43.20940, -51.21770, -50.21886, -46.62918)
lat <- c(-15.799765, -12.579738, -5.498398, -19.183423, -18.512178, -25.252089, -22.913948, -30.034632, -27.242339, -23.543179)
ds_DadosAcessos <- data.frame(char = nom_State, lon, lat, val_Qtd)
还有我的剧情:
library(ggmap)
img_Mapa <- get_map('Brazil', zoom = 4)
ggmap(img_Mapa, extent = "device") +
stat_density2d(data = ds_DadosAcessos,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), size = 0.01,
bins = 16, geom = "polygon") +
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 0.3), guide = FALSE)
很明显热图是根据记录的数量来计算热图的。但我需要热图是由 val_Qtd 列计算的。
也许是这样的。
看到你在问题中的评论,我认为你会想要一个 val_Qtd
的等值线图。我在下面解释了我的代码。希望对您有所帮助。
library(raster)
library(ggmap)
### Get data (map and polygons)
brz <- get_map('Brazil', zoom = 4)
mymap <- getData("GADM", country = "brazil", level = 1)
### I realized that a few state names were not spelled properly.
### I changed them in order to subset polygons in the following
### process.
places <- c("Distrito Federal", "Bahia", "Ceará", "Espírito Santo",
"Minas Gerais", "Paraná", "Rio de Janeiro", "Rio Grande do Sul",
"Santa Catarina", "São Paulo")
ds_DadosAcessos$char <- places
### Subset polygons for the states in your data. Then, convert my map
### to data frame, which is necessary for ggplot2.
mymap <- subset(mymap, NAME_1 %in% places)
temp <- fortify(mymap)
### Reorder ds_DadosAcessos by the state order in mymap
ds_DadosAcessos$order <- match(ds_DadosAcessos$char, mymap@data$NAME_1)
ds_DadosAcessos <- ds_DadosAcessos[order(ds_DadosAcessos$order), ]
### Finally, draw a map
ggmap(brz) +
geom_map(data = temp, map = temp,
aes(x = long, y = lat, group = group, map_id = id),
color = "black", size = 0.2) +
geom_map(data = ds_DadosAcessos, map = temp,
aes(fill = val_Qtd, map_id = unique(temp$id)),
alpha = 0.5) +
scale_fill_gradientn(limits = c(min(ds_DadosAcessos$val_Qtd), max(ds_DadosAcessos$val_Qtd)),
colours = c("blue", "red") ) +
theme(legend.position = "right")
library(ggmap)
library(plyr)
img_Mapa <- get_map('Brazil', zoom = 4)
val_Qtd <- c(34, 10, 11, 7, 55, 18, 33, 16, 16, 249)
nom_State <- c("Distrito Federal","Bahia","Ceara","Espirito Santo","Minas Gerais","Parana","Rio de Janeiro","Rio Grande do Sul","Santa Catarina","Sao Paulo")
lon <- c(-47.86447, -41.70073, -39.32062, -40.30886, -44.55503, -52.02154, -43.20940, -51.21770, -50.21886, -46.62918)
lat <- c(-15.799765, -12.579738, -5.498398, -19.183423, -18.512178, -25.252089, -22.913948, -30.034632, -27.242339, -23.543179)
ds_DadosAcessos <- data.frame(char = nom_State, lon, lat, val_Qtd)
temp <- apply(ds_DadosAcessos, 1, function(x) { data.frame(lon = as.numeric(rep(x[2], log(as.numeric(x[4])))),
lat = as.numeric(rep(x[3], log(as.numeric(x[4])))))})
heatdata2 <- ldply(temp, rbind)
ggmap(img_Mapa, extent = "device") +
stat_density2d(data = heatdata2,
aes(x = lon, y = lat, fill = ..level.. , alpha = ..level..),
geom = "polygon", position = "identity", contour = TRUE, n = 100) +
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 0.3), guide = FALSE)
谁能帮我制作 ggmap(热图)? 我的数据:
val_Qtd <- c(34, 10, 11, 7, 55, 18, 33, 16, 16, 249)
nom_State <- c("Distrito Federal","Bahia","Ceara","Espirito Santo","Minas Gerais","Parana","Rio de Janeiro","Rio Grande do Sul","Santa Catarina","Sao Paulo")
lon <- c(-47.86447, -41.70073, -39.32062, -40.30886, -44.55503, -52.02154, -43.20940, -51.21770, -50.21886, -46.62918)
lat <- c(-15.799765, -12.579738, -5.498398, -19.183423, -18.512178, -25.252089, -22.913948, -30.034632, -27.242339, -23.543179)
ds_DadosAcessos <- data.frame(char = nom_State, lon, lat, val_Qtd)
还有我的剧情:
library(ggmap)
img_Mapa <- get_map('Brazil', zoom = 4)
ggmap(img_Mapa, extent = "device") +
stat_density2d(data = ds_DadosAcessos,
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), size = 0.01,
bins = 16, geom = "polygon") +
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 0.3), guide = FALSE)
很明显热图是根据记录的数量来计算热图的。但我需要热图是由 val_Qtd 列计算的。
也许是这样的。
看到你在问题中的评论,我认为你会想要一个 val_Qtd
的等值线图。我在下面解释了我的代码。希望对您有所帮助。
library(raster)
library(ggmap)
### Get data (map and polygons)
brz <- get_map('Brazil', zoom = 4)
mymap <- getData("GADM", country = "brazil", level = 1)
### I realized that a few state names were not spelled properly.
### I changed them in order to subset polygons in the following
### process.
places <- c("Distrito Federal", "Bahia", "Ceará", "Espírito Santo",
"Minas Gerais", "Paraná", "Rio de Janeiro", "Rio Grande do Sul",
"Santa Catarina", "São Paulo")
ds_DadosAcessos$char <- places
### Subset polygons for the states in your data. Then, convert my map
### to data frame, which is necessary for ggplot2.
mymap <- subset(mymap, NAME_1 %in% places)
temp <- fortify(mymap)
### Reorder ds_DadosAcessos by the state order in mymap
ds_DadosAcessos$order <- match(ds_DadosAcessos$char, mymap@data$NAME_1)
ds_DadosAcessos <- ds_DadosAcessos[order(ds_DadosAcessos$order), ]
### Finally, draw a map
ggmap(brz) +
geom_map(data = temp, map = temp,
aes(x = long, y = lat, group = group, map_id = id),
color = "black", size = 0.2) +
geom_map(data = ds_DadosAcessos, map = temp,
aes(fill = val_Qtd, map_id = unique(temp$id)),
alpha = 0.5) +
scale_fill_gradientn(limits = c(min(ds_DadosAcessos$val_Qtd), max(ds_DadosAcessos$val_Qtd)),
colours = c("blue", "red") ) +
theme(legend.position = "right")
library(ggmap)
library(plyr)
img_Mapa <- get_map('Brazil', zoom = 4)
val_Qtd <- c(34, 10, 11, 7, 55, 18, 33, 16, 16, 249)
nom_State <- c("Distrito Federal","Bahia","Ceara","Espirito Santo","Minas Gerais","Parana","Rio de Janeiro","Rio Grande do Sul","Santa Catarina","Sao Paulo")
lon <- c(-47.86447, -41.70073, -39.32062, -40.30886, -44.55503, -52.02154, -43.20940, -51.21770, -50.21886, -46.62918)
lat <- c(-15.799765, -12.579738, -5.498398, -19.183423, -18.512178, -25.252089, -22.913948, -30.034632, -27.242339, -23.543179)
ds_DadosAcessos <- data.frame(char = nom_State, lon, lat, val_Qtd)
temp <- apply(ds_DadosAcessos, 1, function(x) { data.frame(lon = as.numeric(rep(x[2], log(as.numeric(x[4])))),
lat = as.numeric(rep(x[3], log(as.numeric(x[4])))))})
heatdata2 <- ldply(temp, rbind)
ggmap(img_Mapa, extent = "device") +
stat_density2d(data = heatdata2,
aes(x = lon, y = lat, fill = ..level.. , alpha = ..level..),
geom = "polygon", position = "identity", contour = TRUE, n = 100) +
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 0.3), guide = FALSE)