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

How can I draw XYZ points and depend point size on the Z value in R?

我有一个数据框,其中前两列描述点坐标 (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")

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

ggplot(myData,aes(x=X,y=Y)) +
geom_point(aes(size = sqrt(Z1), alpha = .5))

使用 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)