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
  • 2,880
  • 9
  • 30
  • 37
  • 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 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
  • 564
  • 2
  • 14