0

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
user1701545
  • 5,706
  • 14
  • 49
  • 80

2 Answers2

1

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)

enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0
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)

enter image description here

user1701545
  • 5,706
  • 14
  • 49
  • 80
  • 1
    And the explanation? I suppose `bubble` expects a `SpatialPointsDataFrame` not just `SpatialPoints`? – jbaums Apr 28 '14 at 06:15