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

R新手求助:如何用barplot()绘制y轴入渗x轴火烧且对比放牧的柱状图

How to Create a Grouped Barplot with base R's barplot()

Hey there! Let's walk through creating your desired barplot using base R's barplot() function—no ggplot required. I'll start with a simulated dataset matching your variables (burn treatment, grazing treatment, infiltration) to make this concrete, then explain each step so you can adapt it to your actual data.

Step 1: Understand Your Data Structure

First, let's assume your data looks something like this (I'll simulate it with base R):

# Simulate a dataset matching your variables (replace with your actual data)
set.seed(123) # Ensures reproducible results
your_data <- data.frame(
  burn = rep(c("Unburned", "Burned"), each = 3),  # Burn treatments
  grazing = rep(c("No Grazing", "Light Grazing", "Heavy Grazing"), 2),  # Grazing treatments
  infiltration = c(rnorm(3, 10, 2), rnorm(3, 15, 2))  # Infiltration values
)

Step 2: Aggregate Data (Calculate Means)

Barplots typically show summary statistics (like means) rather than raw individual values. Use base R's tapply() to calculate the mean infiltration for each combination of burn and grazing treatments:

# Calculate mean infiltration per burn + grazing group
mean_infiltration <- tapply(
  your_data$infiltration,
  list(your_data$burn, your_data$grazing),  # Group by burn (rows) then grazing (columns)
  FUN = mean
)

This will create a matrix where rows are burn treatments, columns are grazing treatments, and cells are the mean infiltration values.

Step 3: Create the Grouped Barplot

Now use barplot() with the right parameters to get your desired chart:

# Generate the grouped barplot
barplot(
  mean_infiltration,
  beside = TRUE,  # Critical: makes grazing groups side-by-side (not stacked)
  col = c("#2E8B57", "#FF6347", "#4682B4"),  # Unique colors for each grazing treatment
  ylab = "Infiltration",  # Y-axis label (matches your requirement)
  xlab = "Burn Treatment",  # X-axis label (matches your requirement)
  main = "Infiltration by Burn and Grazing Treatments",  # Optional title
  legend.text = colnames(mean_infiltration),  # Use grazing names for legend
  args.legend = list(x = "topright"),  # Place legend in the top-right corner
  ylim = c(0, max(mean_infiltration) * 1.1)  # Adjust Y-axis to fit all bars
)

Key Parameter Explanations

  • beside = TRUE: This is what lets you compare grazing treatments within each burn group—without it, bars would stack instead of being side-by-side.
  • col: Assigns distinct colors to each grazing treatment so readers can easily tell them apart. Feel free to swap in your preferred colors!
  • ylim: Ensures the top of your tallest bar doesn't get cut off by setting the Y-axis maximum to 10% higher than the highest mean value.
  • legend.text: Automatically uses your grazing treatment names for the legend, so you don't have to type them manually.

Adapting to Your Actual Data

If your data has different burn/grazing categories, or if you want to use sums instead of means, just adjust:

  1. The tapply() call to use FUN = sum instead of mean if needed.
  2. The col vector to match the number of grazing treatments you have.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:14:35