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

自定义网格下物种数量统计、方格中心计算及可视化技术问询

Solution for Grid-Based Species Counting & Visualization

Hey there! Let's walk through solving your problem step by step—we'll group your data into your custom grid, count species per cell, and visualize the cells with coloring based on species counts.

Step 1: Assign Data Points to Grid Cells

First, we need to tag each individual in your dataset with the grid cell it belongs to. We'll use the cut() function to map each point's coordinates to the grid intervals you defined:

library(dplyr)

# Define the grid bins from your existing code
x_bins <- seq(0, n, 5)   # Bins for the x-axis (matches df$y)
y_bins <- seq(min(df$x), max(df$x), 0.6)  # Bins for the y-axis (matches df$x)

# Add grid group columns to your dataset
df <- df %>%
  mutate(
    # Assign df$y to x-axis grid bins
    x_grid = cut(y, breaks = x_bins, include.lowest = TRUE),
    # Assign df$x to y-axis grid bins
    y_grid = cut(x, breaks = y_bins, include.lowest = TRUE)
  )

Step 2: Count Species per Grid Cell

Now we can calculate two useful metrics:

  1. Species abundance: How many individuals of each species are in each cell
  2. Species richness: How many unique species are present in each cell

Option A: Species Abundance per Cell

# Count individuals per species per grid cell
grid_species_abundance <- df %>%
  group_by(x_grid, y_grid, sp) %>%
  summarise(individual_count = n(), .groups = "drop")

# Preview the result
head(grid_species_abundance)

Option B: Species Richness per Cell

This is likely what you want for coloring cells later:

# Count number of unique species per grid cell
grid_species_richness <- df %>%
  group_by(x_grid, y_grid) %>%
  summarise(n_species = n_distinct(sp), .groups = "drop")

# Preview the result
head(grid_species_richness)

Step 3: Calculate Grid Cell Centers

Next, we'll find the center coordinates for each grid cell. Since we know the exact bin boundaries, we can compute centers directly from the bin sequences:

# Calculate center coordinates for each grid bin
x_centers <- x_bins[-length(x_bins)] + diff(x_bins)/2  # X-axis centers
y_centers <- y_bins[-length(y_bins)] + diff(y_bins)/2  # Y-axis centers

# Map center coordinates to our grid summary data
grid_species_richness <- grid_species_richness %>%
  mutate(
    x_center = x_centers[as.integer(x_grid)],
    y_center = y_centers[as.integer(y_grid)]
  )

Step 4: Visualize Grid Cells with Coloring

Finally, we'll plot the grid cells, coloring them based on species richness (you can swap this for abundance if needed). We'll use base R plotting to match your existing workflow:

# Load viridis for nice color scales (optional but recommended)
library(viridis)

# Plot original data points
plot(y = df$x, x = df$y, pch = 21, bg = df$sp, col = df$sp, cex = 0.4,
     xlab = "Y", ylab = "X", main = "Grid-Based Species Richness")

# Add grid lines
abline(v = x_bins, h = y_bins, col = "gray", lty = 2)

# Plot cell centers colored by species richness
points(x = grid_species_richness$x_center, y = grid_species_richness$y_center,
       pch = 22, bg = viridis(length(unique(grid_species_richness$n_species)))[grid_species_richness$n_species],
       cex = 2)

# Add a legend for species richness
legend("topright", legend = unique(grid_species_richness$n_species),
       fill = viridis(length(unique(grid_species_richness$n_species))),
       title = "Number of Species")

Notes

  • If you want to color cells by total individual count instead of species richness, just modify the summary step to calculate total_individuals = n() instead of n_distinct(sp).
  • The viridis package provides colorblind-friendly scales—if you don't want to use it, you can replace viridis() with base R color functions like rainbow() or heat.colors().

内容的提问来源于stack exchange,提问作者M. Beausoleil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:42:43