在上一次R函数运行的绘图顶部绘制线段 [英] Plotting line segments on top of a plot from a previous run of an R function

查看:242
本文介绍了在上一次R函数运行的绘图顶部绘制线段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 R 函数叫做 stock (下面)。我想知道是否可能以任何方式将函数的每次运行结果(即 plot())绘制(即添加)在顶部从之前的函数运行的情节? (代码下面的图片可能会显示这个)

$ $ $ $ $ $ $ $ $ $ c $ stock $ function $ $ $ $ $ $ b循环=长度(s)

I =矩阵(NA,循环,2)

for(i in 1:loop){
I [i, ] =分位数(rbeta(1e2,m,s [i]),c(.025,.975))
}
plot(rep(1:loop,2),I [,1: 2],ty =n,ylim = 0:1,xlim = c(1,loop))

segments(1:loop,I [,1],1: ,2])
}
#使用示例:
stock(m = 2,s = c(1,10,15,20,25,30))
stock (m = 50,s = c(1,10,15,20,25,30))#此运行的结果绘制在
上方的上方, p $ p>

解决方案

<最简单的方法是为选项添加一个参数。作为 segments()默认添加到前一帧,所要做的就是不要做一个新的 plot()
$ $ $





$长度(s)
I =矩阵(NA,循环,2)

for(i in 1:loop){
I [i,] = quantile(rbeta(1e2 ,(m,s [i]),c(.025,.975))
}
if(!add){
plot(rep(1:loop,2),I [ ,1:2],ty =n,ylim = 0:1,xlim = c(1,loop))
}
segments(1:loop,I [,1],1:循环,I [,2],xpd = NA)
}

#使用示例:
set.seed(1)
stock(m = 2, s = c(1,10,15,20,25,30))
stock(m = 50,s = seq(1,90,10),add = TRUE)

< img src =https://i.stack.imgur.com/j2YeA.pngalt =在这里输入图片描述>


I have an R function called stock (below). I was wondering if it might be in any way possible that the result of each run of the function (which is a plot()) be plotted (i.e., added) on top of the plot from the previous run of the function? (the picture below the code may show this)

stock = function(m, s){

 loop = length(s) 

 I = matrix(NA, loop, 2)

for(i in 1:loop){
I[i,] = quantile(rbeta(1e2, m, s[i]), c(.025, .975))
 }
plot(rep(1:loop, 2), I[, 1:2], ty = "n", ylim = 0:1, xlim = c(1, loop))

segments(1:loop, I[, 1], 1:loop, I[, 2])
}
# Example of use:
stock(m = 2, s = c(1, 10, 15, 20, 25, 30))
stock(m = 50, s = c(1, 10, 15, 20, 25, 30)) #The result of this run be plotted on top of previous run above

解决方案

Simplest would be to add an argument for the option. As segments() by default adds to the previous frame, all you have to do is to not do a new plot().

stock = function(m, s, add=FALSE) {

    loop = length(s) 
    I = matrix(NA, loop, 2)

    for(i in 1:loop) {
        I[i,] = quantile(rbeta(1e2, m, s[i]), c(.025, .975))
    }
    if (!add) {
        plot(rep(1:loop, 2), I[, 1:2], ty = "n", ylim = 0:1, xlim = c(1, loop))
    }
    segments(1:loop, I[, 1], 1:loop, I[, 2], xpd = NA)
}

# Example of use:
set.seed(1)
stock(m = 2, s = c(1, 10, 15, 20, 25, 30))
stock(m = 50, s = seq(1, 90, 10), add=TRUE)

这篇关于在上一次R函数运行的绘图顶部绘制线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆