1.调用“csv、txt文件”(选用choose函数,将文本放在指定的文件夹中)
csvpath <- file.choose()
1.1读取文件存放路径
csvpath
#[1] "/Users/yxwyxf/Desktop/文件汇总/听晓伟简说/2021.01.06-R语言生态学数据分析/R语言生态学数据分析案例.csv"
1.2读取csv数据
treedata <- read.csv(csvpath,header = T,row.names =1)
示例数据
2.计算文本几行几列(查验导入数据是否有错)
dim(treedata)
#[1] 41 10
3.分类汇总某一表中数据类别
table(treedata$group)
table(treedata$sp.code)
> table(treedata$group)
Climax Exotic Pioneer Transitional
5 3 21 12
> table(treedata$sp.code)
sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8 sp9
4 3 6 3 7 3 6 6 3
4.筛选核心区的林木
subdata <- subset(treedata, x >=5 & x<=25 & y>=5 & y <=25)
subdata
> subdata <- subset(treedata, x >=5 & x<=25 & y>=5 & y <=25)
> subdata
x y sp.code dbh.cm h.m storey crownwid.m group biomass.kg quality
T12 21.6 5.6 sp5 24.3 16.2 5 4.9 Pioneer 311.95 Poor
T13 20.5 12.4 sp6 11.7 9.4 6 3.1 Transitional 42.40 Excellent
T14 20.0 12.5 sp7 28.1 7.9 6 5.8 Pioneer 203.87 Average
T15 23.4 17.6 sp5 14.3 17.6 5 3.2 Transitional 117.96 Good
T16 15.5 21.5 sp5 13.4 9.4 6 3.2 Climax 55.54 Low
T17 5.8 24.4 sp5 16.4 14.0 5 3.2 Pioneer 123.38 Excellent
T18 6.2 20.1 sp5 19.8 8.8 6 3.7 Pioneer 113.10 Average
T19 9.5 20.5 sp5 20.4 7.4 6 4.1 Pioneer 101.01 Good
T20 10.3 19.0 sp4 20.5 6.0 6 4.4 Transitional 82.79 Excellent
T21 10.2 14.9 sp4 18.2 22.1 4 3.6 Pioneer 239.05 Excellent
T22 16.2 19.8 sp7 28.9 7.1 6 7.2 Climax 193.86 Average
T23 15.3 6.4 sp7 16.2 17.8 5 3.2 Climax 152.90 Average
T24 14.6 6.2 sp7 9.4 13.1 3 2.6 Climax 38.16 Poor
T25 14.8 6.8 sp7 7.9 28.3 4 1.8 Exotic 58.10 Excellent
T26 19.8 9.4 sp8 17.6 20.7 6 3.5 Transitional 209.53 Average
T28 22.5 10.0 sp8 14.0 26.7 1 3.2 Pioneer 171.19 Excellent
T29 8.9 19.2 sp8 8.8 47.6 1 1.9 Pioneer 120.80 Excellent
T32 18.5 15.1 sp9 22.1 29.2 6 4.9 Exotic 464.12 Poor
T33 6.2 13.6 sp9 7.1 11.4 6 1.7 Transitional 19.01 Excellent
T34 18.3 5.4 sp8 17.8 12.0 5 3.5 Transitional 124.58 Average
T35 19.5 9.7 sp7 13.1 14.5 6 3.2 Transitional 87.71 Good
T37 16.5 22.7 sp6 20.7 19.2 4 4.6 Transitional 268.49 Excellent
T38 8.4 12.2 sp5 12.7 21.9 3 3.1 Pioneer 115.78 Average
T39 22.6 9.2 sp1 26.7 28.0 4 5.2 Climax 648.48 Average
5.绘制样地树种分布图,且不同树种用不同颜色表示
sp1data <- subset(treedata, sp.code=="sp1")
sp2data <- subset(treedata, sp.code=="sp2")
sp3data <- subset(treedata, sp.code=="sp3")
sp4data <- subset(treedata, sp.code=="sp4")
sp5data <- subset(treedata, sp.code=="sp5")
sp6data <- subset(treedata, sp.code=="sp6")
sp7data <- subset(treedata, sp.code=="sp7")
sp8data <- subset(treedata, sp.code=="sp8")
sp9data <- subset(treedata, sp.code=="sp9")
opar <- par(mar=c(4,4,3,1))
plot(sp1data$y~sp1data$x,
col=1,
pch=19,
main="",
xlab="x轴x data(m)",
ylab="y轴y data(m)",
xlim=c(0,30),
ylim=c(0,25),
xaxs="I",
yaxs="I")
points(sp2data$y~sp2data$x, col=2, pch=19)
points(sp3data$y~sp3data$x, col=3, pch=19)
points(sp4data$y~sp4data$x, col=4, pch=19)
points(sp5data$y~sp5data$x, col=5, pch=19)
points(sp6data$y~sp6data$x, col=6, pch=19)
points(sp7data$y~sp7data$x, col=7, pch=19)
points(sp8data$y~sp8data$x, col=8, pch=19)
points(sp9data$y~sp9data$x, col=9, pch=19)
rect(5,5,25,20,border="red")
legend("top", title="树种 Tree species", cex=0.8, inset=-0.16, xpd=TRUE,
legend=paste("sp",1:9,sep=""), border="white", bty="n",
col=1:9,pch=19, xjust=2, yjust=1, ncol=9)
5.1结果如下图所示
6.筛选出某一类别的树种(处于4 storey 的 Pioneer 树种)
subset(treedata, storey=="4"&group=="Pioneer")
> subset(treedata, storey=="5"&group=="Pioneer")
x y sp.code dbh.cm h.m storey crownwid.m group biomass.kg quality
T4 25.7 8.0 sp1 29.2 14.3 5 7.6 Pioneer 397.11 Low
T5 28.8 4.0 sp3 11.4 13.4 5 3.0 Pioneer 57.29 Excellent
T6 1.0 7.0 sp3 12.0 16.4 5 3.1 Pioneer 77.57 Average
T7 0.8 5.6 sp3 14.5 19.8 5 3.2 Pioneer 136.34 Good
T10 13.4 0.6 sp3 21.9 18.2 5 4.7 Pioneer 284.79 Average
T12 21.6 5.6 sp5 24.3 16.2 5 4.9 Pioneer 311.95 Poor
T17 5.8 24.4 sp5 16.4 14.0 5 3.2 Pioneer 123.38 Excellent
T36 10.5 0.5 sp6 28.3 12.2 5 7.2 Pioneer 318.60 Excellent
T41 26.2 12.0 sp1 48.8 11.7 5 10.5 Pioneer 903.64 Low
7.将样地内林木按指定要求排序
##将样地中的林木按dbh.cm的数值从大至小排列
treedata[order(treedata$dbh.cm, decreasing=TRUE),]
treedata[order(treedata$h.m),]
8.计算样地内单株林木冠幅面积,并添加到文本中
treedata$area <- pi*((treedata$crownwid.m/2)^2)
> treedata$area <- pi*((treedata$crownwid.m/2)^2)
9.计算单株材积
treedata$volume <- pi*((treedata$dbh.cm/2)^2)/10000*(treedata$h.m+3)*0.45
> treedata$volume <- pi*((treedata$dbh.cm/2)^2)/10000*(treedata$h.m+3)*0.45
10.确定单株径级范围
Drange <- cut(treedata$dbh.cm, breaks=seq(6,50,2),
right=FALSE, include.lowest=TRUE)
treedata$Drange <- Drange
> Drange <- cut(treedata$dbh.cm, breaks=seq(6,50,2),
+ right=FALSE, include.lowest=TRUE)
11.探究胸径与冠幅面积之间的关系
$11.1画出散点图
plot(treedata$dbh.cm, treedata$area )
11.2建立一元线性回归模型
lm.sol <- lm(area~1+dbh.cm, data=treedata)
summary(lm.sol)
Call:
lm(formula = area ~ 1 + dbh.cm, data = treedata)
Residuals:
Min 1Q Median 3Q Max
-11.9931 -5.1934 -0.4529 5.6043 15.8063
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.323 2.209 -7.843 1.54e-09 ***
dbh.cm 1.806 0.102 17.693 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 6.547 on 39 degrees of freedom
Multiple R-squared: 0.8892, Adjusted R-squared: 0.8864
F-statistic: 313 on 1 and 39 DF, p-value: < 2.2e-16
11.3绘图查看拟合效果
opar <- par(mar=c(4,5,1,1))
plot(area~dbh.cm, data=treedata,
xlab="胸径 DBH(cm)",
ylab=expression(paste("冠幅 Crown area", (m^2),sep="")))
abline(lm.sol,lwd=2, col="blue")
text(14,80,expression(paste(italic(y),"=1.806", italic(x), "-17.323")))
text(14,70,expression(paste(italic(R^2),"=0.8892", sep="")))
text(14,60,expression(paste(italic(P),"<0.001", sep="")))