1

I am trying to create a bubble plot in R using the sp package. My data has a lot of missing values ("N/A") and the bubble plot does not seem to like it.

library(sp)
X<-runif(100, min=0, max=1000)
Y<-runif(100, min=0, max=1000)
grade<-c((rnorm(n=50,mean=30, sd=4)), (rep(NA, 50)))
df<-data.frame(X,Y, grade)
coordinates(df)<-~X+Y
bubble(df, "grade", na.rm=TRUE)

When I run this code I am getting an error message "Error in quantile.default(data[, zcol]) : missing values and NaN's not allowed if 'na.rm' is FALSE".

I don't understand because I have said to remove missing values!! I suspect that sp has a slightly different method for dealing with missing values that I have missed

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
LoveMeow
  • 1,141
  • 2
  • 15
  • 26

2 Answers2

4

Try this instead (since the na.rm does not seem to get passed to the proper function):

bubble(df[!is.na(df$grade), ], "grade")

There is also a subset method for 'SpatialPointsDataFrame'-objects:

bubble(subset(df, !is.na(grade)), "grade")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
4

If you read help(bubble) you'll see that there's no na.rm parameter. Just because it works with lm and glm that doesn't mean it will work everywhere. Remember that R is written by hundreds of people and there's no universal requirement to follow some rule that na.rm always works.

Note that bubble has a "..." argument - this will catch your 'na.rm' and pass it on to xyplot - but that doesn't have an na.rm argument either. Not that that matters since the error is thrown up by code in bubble before it even thinks about calling xyplot.

subset is the answer (as already explained)

Spacedman
  • 92,590
  • 12
  • 140
  • 224