简体   繁体   中英

Specifying spatial coordinates in a data.frame

Pretty silly but I can't figure out what I'm doing wrong here:

I have a data.frame with 2 columns:

df = data.frame(x = rep(1, 20), y = runif(20, 10,20))

I then want to set x and y as spatial coordinates so I can plot df in a bubble plot . So I try:

coordinates(df) = c("x","y")

But then:

bubble(df)

gives this error:

Error in data.frame(x@data, x@coords) : 
  arguments imply differing number of rows: 0, 20

For bubble plot to be meaningful, you should probably create a SpatialPointsDataFrame .

library(sp)
df <- data.frame(x = rep(1, 20), y = runif(20, 10,20))
data <- data.frame(variable = runif(20))
coordinates(df) <- ~ x + y
out <- SpatialPointsDataFrame(df, data)
bubble(out)

在此处输入图片说明

library(sp)
set.seed(1)
df = data.frame(x = rep(1, 20), y = runif(20, 10, 20), dummy = rep(0, 20))
coordinates(df) = c("x","y")
bubble(df)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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