0

I have two variables that I want to plot. One is Abundance (y axis), and the other is Northing. I have created a spatial points data frame, converting Northing into Latitude (x axis).

Usually this would be fine for me to plot, however, I want to remove the zeros from and log my Abundance variable (y axis). How could I do this without getting an error like "variable lengths different"?

plot(SP_df$Latitude, df$Abundance)

abline(lm(df$Abundance ~ SP_df$Latitude))
plamut
  • 3,085
  • 10
  • 29
  • 40
  • 1
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run and test the code. – MrFlick Jan 03 '18 at 15:44
  • If this is your data: `x <- SpatialPointsDataFrame(coords=data.frame(x=1:3,y=1:3), data=data.frame(lat=1:3,abu=c(0,10,100)))` you can get rid of the zero rows like this: `x_filter <- x[x$abu > 0, ]`. – qdread Jan 03 '18 at 16:54

1 Answers1

0

Taking the example from @qdread, you can index a SpatialPointsDataFrame just like any other dataframe.

Create the reprex dataframe:

x <- SpatialPointsDataFrame(coords = data.frame(x = 1:10,y = 1:10),
                        data = data.frame(lat = 1:10,abu = exp(0:9)))

Index the dataframe and log transform abundance column:

x.above.zero <- x[x$abu>0,]
x.above.zero$logAbu <- log(x.above.zero$abu)

You can then plot it as you wanted.

plot(x.above.zero$lat, x.above.zero$logAbu)
abline(lm(x.above.zero$logAbu~x.above.zero$lat))

In general, keeping the metadata tied to the spatial data by keeping it all in one object avoids this problem.

m.evans
  • 606
  • 3
  • 15