简体   繁体   English

删除零并记录y轴,以空间点数据框为x轴

[英]Removing zeros and logging y axis, with a spatial points data frame as the x axis

I have two variables that I want to plot. 我有两个要绘制的变量。 One is Abundance (y axis), and the other is Northing. 一个是丰度(y轴),另一个是北向。 I have created a spatial points data frame, converting Northing into Latitude (x axis). 我创建了一个空间点数据框,将北向转换为纬度(x轴)。

Usually this would be fine for me to plot, however, I want to remove the zeros from and log my Abundance variable (y axis). 通常,这对我来说可以绘制,但是,我想从中删除零并记录我的丰度变量(y轴)。 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))

Taking the example from @qdread, you can index a SpatialPointsDataFrame just like any other dataframe. 以@qdread为例,您可以像其他任何数据框一样索引SpatialPointsDataFrame。

Create the reprex dataframe: 创建reprex数据框:

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. 通常,通过将所有元数据都保留在一个对象中来将其与空间数据绑定在一起可以避免此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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