ggmap带有值的热图 [英] ggmap Heatmap with value
本文介绍了ggmap带有值的热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据:
val_Qtd < - c(34,10,11,7,55,18,33 (Distrito Federal,Bahia,Ceara,Espirito Santo,Minas Gerais,Parana,里约热内卢, 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
。我在下面解释了我的代码。
库(栅格)
库(ggmap)
###获取数据(地图和多边形)
brz< - get_map('Brazil',zoom = 4)
mymap< - getData(GADM,country =brazil,level = 1)
###我意识到一些州名拼写不正确。
###我改变了它们以便在以下
###过程中对多边形进行子集划分。
地点<-c(Distrito Federal,Bahia,Ceará,EspíritoSanto,
米纳斯吉拉斯,巴拉那,里约热内卢 ,Rio Grande do Sul,
Santa Catarina,SãoPaulo)
ds_DadosAcessos $ char < - places
###子集多边形为您的数据中的状态。然后,将我的映射
###转换为数据帧,这对于ggplot2是必需的。
mymap< - subset(mymap,NAME_1%in%places)
temp< - fortify(mymap)
###按状态重新排序ds_DadosAcessos order in mymap
ds_DadosAcessos $ order< - match(ds_DadosAcessos $ char,mymap @ data $ NAME_1)
ds_DadosAcessos< - ds_DadosAcessos [order(ds_DadosAcessos $ order),]
###最后,绘制地图
ggmap(brz)+
geom_map(data = temp,map = temp,$ b $ 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)),
颜色= c(蓝色,红色))+
主题(legend.position =right)
Can anyone help me with the ggmap (heatmap)? My data:
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)
And my plot:
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)
Obviously the heatmap is using the quantity of records to calc heatmap. But I need the heatmap is calculated by val_Qtd column.
Maybe something like this.
解决方案
Seeing your comments in your question, I thought you would want a chropleth map with val_Qtd
. I explained my codes below. I hope this will help you.
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")
这篇关于ggmap带有值的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文