简体   繁体   English

使用字符串作为绘图选项

[英]Use character string as options for plotting

Say I have the different options for plotting saved as a character. 说我有不同的打印选项保存为角色。 For the sake of this question, let's assume I have received tons of these from elsewhere. 为了这个问题,让我们假设我从其他地方收到了很多。

option1 = "type = 'p', col = 'red', pch = 19, cex = 2"
option2 = "type = 'l', lty = 2, lwd = 2, col = 'blue'"

I suppose I'd have to parse them and identify the different options. 我想我必须解析它们并确定不同的选项。 But before I do that I was wondering if there is a way to use them directly when plotting. 但是在我这样做之前,我想知道是否有一种方法可以在绘图时直接使用它们。

Here is a code that doesn't work. 这是无效的代码。

#Data
set.seed(42)
x = rnorm(20)
y = rnorm(20)

plot(x, y, option1)
plot(x, y, option2)

How about this: 这个怎么样:

eval(parse(text = paste0("plot(x, y, ", option1, ")")))
eval(parse(text = paste0("plot(x, y, ", option2, ")")))

您可以将绘图调用构造为字符串并评估其解析形式:

eval(parse(text = paste0("plot(x,y,",option1,")")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM