如何给出Y轴限制和断开作为argumnet

时间:2018-05-16 14:11:02

标签: r ggplot2

我编写了一个R脚本,它将数据文件名作为参数并生成图形。我想给y轴限制并作为参数中断,但是得到了跟随错误

Error in del/by : non-numeric argument to binary operator
Calls: scale_y_continuous ... continuous_scale -> check_breaks_labels -> s    eq -> seq.default

显然我的脚本在这里寻找数字

scale_y_continuous(expand=c(0,0), limits = c(0, args[4]),breaks = seq(0, args[4], by = args[5]))

这个问题有解决方法吗?

我的数据文件看起来像这样

> Data2
  treatment N     value        sd        se        ci
1        AL 3 2.4066667 0.3950105 0.2280594 0.9812606
2        BL 3 5.8000000 1.0023971 0.5787343 2.4900925
3        CL 3 0.7333333 0.2516611 0.1452966 0.6251609

虽然脚本如下

#!/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")

# create graph
png(args[2], units="in", family="Times New Roman",  width=1.2, height=1.2, res=300, pointsize = 2) #pointsize is font size| increase image size to see the key
ggplot(Data2, aes(x=treatment, y=value, fill=Data2$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, args[4]),breaks = seq(0, args[4], by = args[5]))+
theme_ms()

dev.off()

感谢。

1 个答案:

答案 0 :(得分:1)

您需要先将参数转换为数值。 args - 是一个字符向量。试试:

  ggplot(Data2, aes(x=treatment, y=value, fill=Data2$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(as.numeric(args[3]))+
  labs(y="Relative expression",x="")+
  scale_y_continuous(expand=c(0,0), 
                     limits = c(0, as.numeric(args[4])),
                     breaks = seq(0, as.numeric(args[4]), by = as.numeric(args[5])))+
  theme_ms()
相关问题