简化R中的plot()代码

时间:2016-08-15 14:50:32

标签: r plot

当我将多条线添加到绘图中时,代码会像这样爆炸。

plot_pch = 16
plot_ltya = 'solid'
plot_ltyb = 'dotted'

plot(df[cond1, 'field'], 
     type='o',
     pch=plot_pch,
     plot_ltya)
lines(df[cond2, 'field'],
      type='o',      
      pch=plot_pch,
      lty=plot_ltya)
lines(df[cond3, 'field'],
      type='o',      
      pch=plot_pch,
      lty=plot_ltya)
lines(df[cond4, 'field'],
      type='o',      
      pch=plot_pch,
      lty=plot_ltyb)
lines(df[cond5, 'field'],
      type='o',      
      pch=plot_pch,
      lty=plot_ltyb)

有没有办法通过各种全局设置简单地解决这个问题?

setglobal(pch=plot_pch)

1 个答案:

答案 0 :(得分:2)

如果要在pch会话的剩余时间内更改图形参数(如R),您可以执行以下操作:

plot_pch <- 16
par(pch = plot_pch)

设置pch值后,另一种简化此调用的方法是使用mapply函数:

plot(df[cond1, 'field'], 
     type='o',
     pch=plot_pch,
     lty = 'solid')

mapply(function(df, lty) lines(df, type = 'o', lty = lty), 
df = list(df[cond2, 'field'], df[cond3, 'field'], 
          df[cond4, 'field'], df[cond5, 'field']), 
lty = rep(c('solid','dotted'), each = 2))