简体   繁体   中英

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,")")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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