如何绘制XYZ点并将点大小依赖于R中的Z值?

时间:2017-03-09 06:43:43

标签: r plot maps geospatial

我有数据框,其中两个第一列描述点坐标(X,Y),而下两列存储值。

我想将它们(在地图上)绘制为点,但我希望点大小取决于值(例如Z1值)。

我怎么能在R?中做到这一点?

我知道有一个rasterFromXYZ命令,但我不知道对应点。

我的样本数据如下:

myData <- structure(list(X = c(20.688, 18.905, 19.086, 21.135, 21.979, 
22.495), Y = c(52.387, 53.747, 52.495, 53.466, 54.093, 52.879
), Z1 = c(1050L, 359L, 424L, 393L, 1478L, 573L), Z2 = c(48L, 
38L, 20L, 150L, 138L, 120L)), .Names = c("X", "Y", "Z1", "Z2"
), row.names = c(NA, 6L), class = "data.frame")

2 个答案:

答案 0 :(得分:3)

您可以使用大多数绘图工具执行此操作,例如使用ggplot:

onblur

答案 1 :(得分:2)

使用ggmap库,您可以执行以下操作:

# set up the sample dataset
myData <- structure(list(X = c(20.688, 18.905, 19.086, 21.135, 21.979, 
                               22.495), Y = c(52.387, 53.747, 52.495, 53.466, 54.093, 52.879
                               ), Z1 = c(1050L, 359L, 424L, 393L, 1478L, 573L), Z2 = c(48L, 
                                                                                       38L, 20L, 150L, 138L, 120L)), .Names = c("X", "Y", "Z1", "Z2"
                                                                                       ), row.names = c(NA, 6L), class = "data.frame")

# load the ggmap library
library(ggmap)

# set the part of the world to show
location <- c(lon = mean(myData[, "X"]), lat = mean(myData[, "Y"]))

# choose map type
maptype = "terrain"

# get the map from google (choose zoom you like, or let it choose manually)
map <- get_map(location = location, source = "google",
               maptype = maptype, crop = FALSE, zoom = 6)

# print the map
ggmap(map) +
        # add the points, where X is longitude, Y latitude and size is calculated based on Z1 (you might choose some other scaling of Z1)
        geom_point(aes(x = X, y = Y, size = 1+Z1/sum(Z1)), data = myData)