R脚本传入Y轴范围与刻度间隔参数时出错的解决方法
That error you're seeing happens because command-line arguments passed via commandArgs() are always strings by default, but scale_y_continuous() and the seq() function need numeric values to work properly. When you try to use args[4] and args[5] directly in those calculations, R tries to do math on character strings—hence the "non-numeric argument" error.
Here's how to fix it step by step:
1. Convert Command-Line Args to Numeric Type
First, add code to convert your Y-axis parameters (args[4] and args[5]) to numeric values. It's also a good idea to set fallback defaults in case those arguments aren't provided, so your script doesn't break unexpectedly.
Insert this right after you load your data:
# Handle Y-axis parameters (convert to numeric, set defaults if missing) y_max <- if (length(args) >= 4) as.numeric(args[4]) else max(Data2$value + Data2$se) * 1.1 y_break_interval <- if (length(args) >= 5) as.numeric(args[5]) else y_max / 5
- The default
y_maxcalculates a value slightly larger than your tallest bar + error bar, so your data doesn't get cut off. - The default
y_break_intervalsplits the Y-axis into 5 even ticks, which is a clean, readable default.
2. Update the scale_y_continuous Call
Replace your original scale_y_continuous line with this, using the converted numeric variables instead of raw args:
scale_y_continuous(expand=c(0,0), limits = c(0, y_max), breaks = seq(0, y_max, by = y_break_interval))
3. Full Modified Script Snippet
Here's how the relevant part of your script will look after changes:
#!/usr/bin/env Rscript args = commandArgs(trailingOnly=TRUE) # test if there is at least one argument: if not, return an error if (length(args)==0) { stop("At least one argument must be supplied (input file).\n", call.=FALSE) } else if (length(args)==1) { # default output file args[2] = "out.png" } library(Rmisc) library(ggplot2) Data2 <- read.csv(args[1], header=T) source("https://gist.githubusercontent.com/sanjaysingh765/fcfaf58e2de7874a9c646097fe4c5c38/raw/ggplot2_theme2.R") # Handle Y-axis parameters (convert to numeric, set defaults if missing) y_max <- if (length(args) >= 4) as.numeric(args[4]) else max(Data2$value + Data2$se) * 1.1 y_break_interval <- if (length(args) >= 5) as.numeric(args[5]) else y_max / 5 # create graph png(args[2], units="in", family="Times New Roman", width=1.2, height=1.2, res=300, pointsize = 2) ggplot(Data2, aes(x=treatment, y=value, fill=treatment))+ geom_bar(colour="black",stat="identity",position=position_dodge(),size=.1,width=.5) + geom_errorbar(aes(ymax=value+se, ymin=value-se),width=0.1, size=0.2, color="black")+ scale_fill_brewer(palette="Set3",guide=FALSE) + ggtitle(args[3])+ labs(y="Relative expression",x="")+ scale_y_continuous(expand=c(0,0), limits = c(0, y_max), breaks = seq(0, y_max, by = y_break_interval))+ theme_ms() dev.off()
4. How to Call the Script
When running from the command line, pass numeric values for the Y-axis max and break interval like this:
Rscript your_script_name.R data.csv output_plot.png "Your Plot Title" 7 1.4
- The 4th argument (
7) is your Y-axis upper limit - The 5th argument (
1.4) is the tick interval
This should resolve the error and let you control the Y-axis via command-line parameters smoothly.
内容的提问来源于stack exchange,提问作者pali

