简体   繁体   English

R中的多边形贴图(空间数据)

[英]Polygon Maps in R (spatial data)

Could someone help me create a polygon map according to the real estate dataset( https://docs.google.com/spreadsheets/d/1g_awI6hnD80IVeZDOWpvU_NHdHxsk_Uyo8HML9p-yHo/edit#gid=0 )? 有人可以根据房地产数据集( https://docs.google.com/spreadsheets/d/1g_awI6hnD80IVeZDOWpvU_NHdHxsk_Uyo8HML9p-yHo/edit#gid=0 )帮助我创建多边形地图吗? I want to get a regional map of Sacramento with different colors representing different real estate prices. 我想获取一份萨克拉曼多的区域地图,用不同的颜色代表不同的房地产价格。

The code that I provided below came from a sample polygon R code. 我在下面提供的代码来自示例多边形R代码。 I can't figure out how the "merge" function works in polygon maps. 我无法弄清楚“合并”功能在多边形地图中的工作方式。

Thank you, 谢谢,

#load libraries
library(ggmap)
library(ggplot2) 
library(gpclib)
library(rgeos)


getClass("Polygon")
getClass("SpatialPolygons")
realestate.f <- fortify(realestate, region = "zip")
realestate.f <- merge(realestate.f, realestate, by.x = "longitude", by.y = "latitude")
head(realestate.f)

Map <- ggplot(realestate.f, aes(longitude, latitude, price)) + 
geom_polygon() + coord_equal() + 
labs(x = "longitude", y = "latitude", "price") + 
ggtitle("Sacramento Real Estate Prices")

Map + scale_fill_gradient(low = "white", high = "black")`

@Yun Zhou: it looks like you are starting with point data. @Yun Zhou:看来您是从点数据开始的。 I believe you first need to convert your dataframe to a SpatialPointsDataFrame before you can convert the points to polygonal data (or some density function). 我相信您首先需要将数据框转换为SpatialPointsDataFrame,然后才能将点转换为多边形数据(或某些密度函数)。

#dataframe from csv
df.in

# Coerce to SpatialPointsDataFrame
coordinates(df.in) = c("latitude", "longitude") 
class(df.in) #it should be a sptial points data frame
plot(df.in)

Plot: 情节:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM