- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目标是构建类似的东西
http://rentheatmap.com/sanfrancisco.html
我得到了带有 ggmap 的 map ,并且能够在它上面绘制点。
library('ggmap')
map <- get_map(location=c(lon=20.46667, lat=44.81667), zoom=12, maptype='roadmap', color='bw')
positions <- data.frame(lon=rnorm(100, mean=20.46667, sd=0.05), lat=rnorm(100, mean=44.81667, sd=0.05), price=rnorm(10, mean=1000, sd=300))
ggmap(map) + geom_point(data=positions, mapping=aes(lon, lat)) + stat_density2d(data=positions, mapping=aes(x=lon, y=lat, fill=..level..), geom="polygon", alpha=0.3)
positions$price_cuts <- cut(positions$price, breaks=5)
ggmap(map) + stat_density2d(data=positions, mapping=aes(x=lon, y=lat, fill=price_cuts), alpha=0.3, geom="polygon")
positions <- data.frame(lon=rnorm(10000, mean=20.46667, sd=0.05), lat=rnorm(10000, mean=44.81667, sd=0.05), price=rnorm(10, mean=1000, sd=300))
positions$price <- ((20.46667 - positions$lon) ^ 2 + (44.81667 - positions$lat) ^ 2) ^ 0.5 * 10000
positions <- data.frame(lon=rnorm(10000, mean=20.46667, sd=0.05), lat=rnorm(10000, mean=44.81667, sd=0.05))
positions$price <- ((20.46667 - positions$lon) ^ 2 + (44.81667 - positions$lat) ^ 2) ^ 0.5 * 10000
positions <- subset(positions, price < 1000)
positions$price_cuts <- cut(positions$price, breaks=5)
ggmap(map) + geom_hex(data=positions, aes(fill=price_cuts), alpha=0.3)
test<-read.csv('test.csv')
ggplot(data=test, aes(lon, lat, fill=price_cuts)) + stat_bin2d(, alpha=0.7) + geom_point() + scale_fill_brewer(palette="Blues")
最佳答案
这是我的方法。 geom_hex
方法很好。当它出来时,我真的很喜欢它。我仍然。由于您提出了更多要求,因此我尝试了以下操作。我认为我的结果类似于 stat_density2d
.但是,我可以避免你遇到的问题。我基本上自己创建了一个 shapefile 并绘制了多边形。我按价格区域 (price_cuts) 对数据进行子集化,并从边缘到区域中心绘制多边形。这种方法在EDIT 1和2的路线上。如果你想绘制一个大面积的 map ,我认为距离你的最终目标还有一段距离。但是,我希望这能让你继续前进。最后,我要感谢几个 SO 用户,他们提出了与多边形相关的好问题。没有他们,我无法想出这个答案。
library(dplyr)
library(data.table)
library(ggmap)
library(sp)
library(rgdal)
library(ggplot2)
library(RColorBrewer)
### Data set by the OP
positions <- data.frame(lon=rnorm(10000, mean=20.46667, sd=0.05), lat=rnorm(10000, mean=44.81667, sd=0.05))
positions$price <- ((20.46667 - positions$lon) ^ 2 + (44.81667 - positions$lat) ^ 2) ^ 0.5 * 10000
positions <- subset(positions, price < 1000)
### Data arrangement
positions$price_cuts <- cut(positions$price, breaks=5)
positions$price_cuts <- as.character(as.integer(positions$price_cuts))
### Create a copy for now
ana <- positions
### Step 1: Get a map
map <- get_map(location=c(lon=20.46667, lat=44.81667), zoom=11, maptype='roadmap', color='bw')
### Step 2: I need to create SpatialPolygonDataFrame using the original data.
### http://stackoverflow.com/questions/25606512/create-polygon-from-points-and-save-as-shapefile
### For each price zone, create a polygon, SpatialPolygonDataFrame, and convert it
### it data.frame for ggplot.
cats <- list()
for(i in unique(ana$price_cuts)){
foo <- ana %>%
filter(price_cuts == i) %>%
select(lon, lat)
ch <- chull(foo)
coords <- foo[c(ch, ch[1]), ]
sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))
bob <- fortify(sp_poly)
bob$area <- i
cats[[i]] <- bob
}
cathy <- as.data.frame(rbindlist(cats))
### Step 3: Draw a map
### The key thing may be that you subet data for each price_cuts and draw
### polygons from outer side given the following link.
### This link was great. This is exactly what I was thinking.
### http://stackoverflow.com/questions/21748852/choropleth-map-in-ggplot-with-polygons-that-have-holes
ggmap(map) +
geom_polygon(aes(x = long, y = lat, group = group, fill = as.numeric(area)),
alpha = .3,
data = subset(cathy, area == 5))+
geom_polygon(aes(x = long, y = lat, group = group, fill = as.numeric(area)),
alpha = .3,
data =subset(cathy, area == 4))+
geom_polygon(aes(x = long, y = lat, group = group, fill = as.numeric(area)),
alpha = .3,
data = subset(cathy, area == 3))+
geom_polygon(aes(x = long, y = lat, group = group, fill = as.numeric(area)),
alpha = .3,
data = subset(cathy, area == 2))+
geom_polygon(aes(x = long, y = lat, group = group, fill = as.numeric(area)),
alpha= .3,
data = subset(cathy, area == 1))+
geom_point(data = ana, aes(x = lon, y = lat), size = 0.3) +
scale_fill_gradientn(colours = brewer.pal(5,"Spectral")) +
scale_x_continuous(limits = c(20.35, 20.58), expand = c(0, 0)) +
scale_y_continuous(limits = c(44.71, 44.93), expand = c(0, 0)) +
guides(fill = guide_legend(title = "Property price zone"))
关于r - 带有 ggmap 的 R 中自定义属性的地理热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847188/
我想要类似于以下伪代码的东西: while input is not None and timer = 5: print "took too long" else: print inp
如何将 MainEngine Observable 转换为 Cold?来自这个例子: public IObservable MainEngine { get
自从手表被发明以来,表盘的方圆之争就始终没有停下来过,在漫长的岁月中,无论是方形还是圆形表盘,人们都为其寻找到足够多的设计元素,让其肆意成长,这种生机与活力后来也延续到了智能手表上,在2014年,这
我正在学习 CUDA,试图解决一些标准问题。例如,我正在使用以下代码求解二维扩散方程。但我的结果与标准结果不同,我无法弄清楚。 //kernel definition __global__ void
我的 Web 应用程序使用 native dll 来实现其部分功能(其位置在 PATH 中提供)。一切正常,直到我对 WAR 进行更改并且 JBoss 热部署此 WAR。此时dll已经找不到了,需要手
我看到这个问题here 。这是关于实现每个发出的项目的延迟。这是根据accepted answer如何实现的: Observable.zip(Observable.range(1, 5) .g
我最近一直在进行冷迁移...这意味着我无法在进行迁移时从应用程序级别读取/写入数据库(维护页面)。 这样就不会因为更改结构而发生错误,而且如果负载很大,我也不希望 mysql 在迁移过程中崩溃。 我的
我是一名优秀的程序员,十分优秀!