r - 如何给 Y 轴限制和中断作为 argumnet
问题描述
我编写了一个以数据文件名作为参数并生成图形的 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()
谢谢。
解决方案
您需要先将参数转换为数值。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()
推荐阅读
- javascript - 无头 Chrome:链接的 CSS 不呈现
- css - 如何根据祖先 div 将样式应用于后代 div?
- wordpress - 具有相同用户的两个 wordpress 数据库
- javascript - 成功提交后进行验证
- c++ - 如何在我的基本计算器中循环遍历数组
- protractor - 量角器不等待模态
- c++ - “错误:没有在类 '...' 中声明的 '...' 成员函数”仅在 g++ 中使用 -mwindows 选项时
- python - 使用 selenium 和手动单击时结果不一致
- video - FFMPEG:修复低运动区域的口吃
- google-cloud-platform - 谷歌云计算负载平衡和自动缩放信息不是为系统管理员类型编写的