我试图使用一个for循环在R中并排绘制3条曲线,但是没有收到任何错误消息,没有显示什么?
我怀疑某些东西应该为第一条曲线提供一个初步的绘图平台,然后可以添加第二和第三条曲线。例如,初始绘图平台可以是plot.new()吗?
注意:我确实试图避免使用长if else语句作为解决方案。
这里是我的R代码:
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 )
}发布于 2017-06-08 23:12:11
既然您想避免使用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 )发布于 2017-06-08 22:35:06
第一次插入发生错误,因为曲线不存在。如果您想要一个简单的解决方案,请遵循下面的解决方案:
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 )
}
}发布于 2017-06-08 23:05:09
对我有用的是以下几点:
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)
}https://stackoverflow.com/questions/44446417
复制相似问题