R 中的多边形地图(空间数据)
Polygon Maps in R (spatial data)
谁能帮我根据房地产数据集制作多边形地图(https://docs.google.com/spreadsheets/d/1g_awI6hnD80IVeZDOWpvU_NHdHxsk_Uyo8HML9p-yHo/edit#gid=0)?我想要一张萨克拉门托的区域地图,不同的颜色代表不同的房地产价格。
我在下面提供的代码来自示例多边形 R 代码。我不知道 "merge" 函数在多边形地图中是如何工作的。
谢谢,
#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:看来你是从点数据入手的。我相信您首先需要将数据框转换为 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)
谁能帮我根据房地产数据集制作多边形地图(https://docs.google.com/spreadsheets/d/1g_awI6hnD80IVeZDOWpvU_NHdHxsk_Uyo8HML9p-yHo/edit#gid=0)?我想要一张萨克拉门托的区域地图,不同的颜色代表不同的房地产价格。
我在下面提供的代码来自示例多边形 R 代码。我不知道 "merge" 函数在多边形地图中是如何工作的。
谢谢,
#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:看来你是从点数据入手的。我相信您首先需要将数据框转换为 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)