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

使用barplot绘制柱状图时出现未使用参数错误

Fixing Your Barplot Error in R

First, let's break down the error you're seeing:

Error in [.data.frame(Holly, , 2, 3, 4, 5) : unused arguments (4, 5)

This happens because subsetting multiple columns in a data frame requires combining column indices into a single vector, not passing them as separate arguments. Your code Holly[,2,3,4,5] is incorrect—R interprets 3, 4, and 5 as extra, unused arguments to the [ function instead of part of your column selection.

Step 1: Correct the Column Subsetting

To select columns 2 through 5 properly, use either:

  • A colon for a continuous sequence: Holly[,2:5]
  • A vector of specific indices: Holly[,c(2,3,4,5)]

But fixing the subsetting alone won't get you the plot you want—right now, passing Holly[,2:5] to barplot() would create a grouped barplot of each tree's values across months, not the average of each month across all trees.

Step 2: Calculate Column Means First

You need to compute the mean for each of the four columns first. Use R's built-in colMeans() function for this:

# Calculate the mean of each month column (columns 2-5)
monthly_means <- colMeans(Holly[,2:5])

Step 3: Plot the Means with barplot()

Now pass your computed means to barplot(), and customize the axis labels for clarity:

barplot(monthly_means,
        names.arg = c("1m", "2m", "3m", "4m"), # Or use colnames(Holly)[2:5] for full labels
        main = "Average Number Across All Trees by Month",
        xlab = "Month",
        ylab = "Average Value",
        col = "lightgreen") # Add color for readability

Full Working Example

Here's the complete code using your dataset, including loading the data correctly:

# Create your data frame
Holly <- data.frame(
  Tree = paste0("Tree_", 1:20),
  Avg_number_1m = c(15.2,16.2,14.4,14.6,15.2,14.0,13.8,10.8,12.4,15.6,7.6,13.8,10.8,15.2,16.2,12.4,14.6,15.2,14.0,11.0),
  Avg_number_2m = c(15.0,15.4,9.2,5.6,13.0,12.0,7.8,5.8,9.6,11.0,7.4,7.8,5.8,15.0,15.4,9.2,5.6,13.0,14.4,5.2),
  Avg_number_3m = c(15.2,14.2,3.2,10.4,7.4,13.0,7.2,4.4,6.8,7.2,9.0,7.2,4.4,15.2,14.2,3.2,10.4,7.4,13.2,4.4),
  Avg_number_4m = c(12.0,15.4,1.6,9.2,3.0,11.2,2.0,2.4,2.6,1.8,1.8,2.0,1.6,12.0,15.0,1.6,9.2,3.0,13.8,0.8)
)

# Compute monthly averages
monthly_means <- colMeans(Holly[,2:5])

# Generate the barplot
barplot(monthly_means,
        names.arg = sub("Avg_number_", "", colnames(Holly)[2:5]), # Clean up label text
        main = "Monthly Average Number Across All Trees",
        xlab = "Month",
        ylab = "Average Value",
        col = "skyblue")

Key Takeaways

  • Fix subsetting syntax: Always use a vector (like 2:5 or c(2,3,4,5)) to select multiple columns from a data frame.
  • Compute summary stats first: barplot() doesn't calculate means automatically—use colMeans() to get the values you want to plot.
  • Customize labels: Adjust names.arg to make your plot's axes clear and easy to read.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 18:57:44