如何提供一个初始绘图平台来在R中添加额外的图?

时间:2017-06-08 22:16:23

标签: r plot

我尝试使用plot.new()循环在R中绘制3条并排曲线,但没有显示任何错误消息?

我怀疑某些东西应该为第一条曲线提供初始绘图平台,然后可以添加第二个和第三个绘图。初始绘图平台可以是if吗?

注意:我绝对试图避免使用长else for(i in 1:3){ p = c(.1, .5, .9)[i] col = c("red3", "green3", "blue")[i] curve( dbinom(x, size = 100, prob = p), add = T, ty = "h", xlim = c(0, 100), col = col, xlab = "Number of Agreements", ylab = "Probability", las = 1 ) } 个语句作为解决方案。

这是我的R代码:

Attribute information for this '@Service' instance:Unspecified attributes:value

3 个答案:

答案 0 :(得分:1)

既然您想避免if语句,那么

怎么样?
p = c(.1, .5, .9)
col = c("red3", "green3", "blue")

for(i in 1:3) curve(dbinom(x, size = 100, prob = p[i]), add = i!=1, 
    ty = "h", xlim = c(0, 100), col = col[i], 
    xlab = "Number of Agreements", ylab = "Probability", las = 1 )

答案 1 :(得分:0)

第一次插入发生错误,因为曲线不存在。如果您想要一个简单的解决方案,请按照下面的说明进行操作:

for(i in 1:3){

  if(i == 1){
  p = c(.1, .5, .9)[i]
  col = c("red", "green", "blue")[i]

  curve( dbinom(x, size = 100, prob = p), ty = "h", xlim = c(0, 100), 
         col = col, xlab = "Number of Agreements", ylab = "Probability", las = 1 )
  }else{
    p = c(.1, .5, .9)[i]
    col = c("red", "green", "blue")[i]

    curve( dbinom(x, size = 100, prob = p), add = T, ty = "h", xlim = c(0, 100), 
           col = col, xlab = "Number of Agreements", ylab = "Probability", las = 1 )
  }

}

答案 2 :(得分:0)

对我有用的是:

for(i in 1:3){

  p = c(.1, .5, .9)[i]
col = c("red3", "green3", "blue")[i]

curve( dbinom(x, size = 100, prob = p), 0, 100, add = ifelse(i > 1, T, F), ty = "h", xlim = c(0, 100), 
   col = col, ylab = "Probability", xlab = "Number of Agreements", las = 1)
}