You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中对数据框内多组树木数据进行绘图与曲线拟合?

批量处理树木数据的绘图与曲线拟合需求

原始数据

tree_data <- data.frame(
  tree = c(3,4,5,7),
  sdepth = c(2,2,2,2),
  shallow_avg = c(0.0857, 0.142, 0.0119, 0.0217),
  ddepth = c(3.5,3.5,3.5,3.5),
  deep_avg = c(0.0454, 0.0991, 0.00498, 0.0169),
  swdepth = c(3.7,4.1,5.7,5.1),
  sw_avg = c(0,0,0,0)
)

需求说明

对数据框中每棵独立的树木(按tree列区分),分别生成包含以下4个(x,y)点的数据集:

  • (0, 0)
  • (sdepth, shallow_avg)
  • (ddepth, deep_avg)
  • (swdepth, sw_avg)

基于每个树木的数据集,完成5次多项式曲线拟合并单独绘图,替代手动单棵处理的低效方式。

现有手动处理代码

sample_data <- data.frame(x = c(0, 2, 3.5, 4.7), y = c(0, 0.0679, 0.0367, 0))

# 拟合5次多项式模型
linear_model5 <- lm(y~poly(x,5,raw=TRUE), data=sample_data)

# 绘制散点图
plot(sample_data$x, sample_data$y)

# 生成x轴预测值并绘制拟合曲线
x_axis <- seq(1, 10, length=10)
lines(x_axis, predict(linear_model5, data.frame(x=x_axis)), col='orange')

批量处理解决方案

方案1:使用tidyverse工具链(推荐,结构化处理)

依赖dplyr、tidyr、purrr和ggplot2包,适合后续扩展分析:

library(tidyverse)

# 1. 整理每棵树的(x,y)点数据
tree_points <- tree_data %>%
  rowwise(tree) %>%
  summarize(
    x = c(0, sdepth, ddepth, swdepth),
    y = c(0, shallow_avg, deep_avg, sw_avg),
    .groups = "drop"
  ) %>%
  unnest(c(x, y))

# 2. 批量拟合模型并生成预测数据
tree_models <- tree_points %>%
  group_by(tree) %>%
  nest() %>%
  mutate(
    # 拟合5次多项式模型
    model = map(data, ~lm(y ~ poly(x, 5, raw = TRUE), data = .x)),
    # 生成平滑的预测序列(范围覆盖数据点并略有扩展)
    pred_data = map2(model, data, ~{
      x_axis <- seq(min(.y$x)-0.5, max(.y$x)+0.5, length.out = 100)
      tibble(x = x_axis, y_pred = predict(.x, newdata = tibble(x = x_axis)))
    })
  )

# 3. 批量生成并输出绘图
plots <- tree_models %>%
  mutate(
    plot = map2(data, pred_data, ~{
      ggplot(.x, aes(x, y)) +
        geom_point(size = 2, color = "blue") +
        geom_line(data = .y, aes(x, y_pred), color = "orange", linewidth = 1) +
        labs(title = paste("Tree", unique(.x$tree)), 
             x = "Depth", y = "Average Value") +
        theme_bw()
    })
  )

# 打印所有绘图
walk(plots$plot, print)

方案2:使用Base R(轻量直接)

无需额外包依赖,适合快速实现:

# 按tree列拆分数据为列表
tree_list <- split(tree_data, tree_data$tree)

# 循环处理每棵树
for (tree_idx in seq_along(tree_list)) {
  current_tree <- tree_list[[tree_idx]]
  
  # 构造当前树的(x,y)点
  x_vals <- c(0, current_tree$sdepth, current_tree$ddepth, current_tree$swdepth)
  y_vals <- c(0, current_tree$shallow_avg, current_tree$deep_avg, current_tree$sw_avg)
  sample_data <- data.frame(x = x_vals, y = y_vals)
  
  # 拟合5次多项式模型
  linear_model5 <- lm(y ~ poly(x, 5, raw = TRUE), data = sample_data)
  
  # 绘制图形
  plot(sample_data$x, sample_data$y, 
       main = paste("Tree", current_tree$tree),
       xlab = "Depth", ylab = "Average Value",
       pch = 16, col = "blue")
  
  # 生成平滑x轴并绘制拟合曲线
  x_axis <- seq(min(x_vals)-0.5, max(x_vals)+0.5, length.out = 100)
  lines(x_axis, predict(linear_model5, data.frame(x = x_axis)), 
        col = "orange", lwd = 2)
}

内容的提问来源于stack exchange,提问作者DCB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 08:44:55