【问题标题】:How to graph multiple columns from matrix using plot function如何使用绘图功能从矩阵中绘制多列
【发布时间】:2021-11-18 16:10:19
【问题描述】:

我正在尝试使用 plot 绘制矩阵列,但看起来我在某处犯了错误。我在网上找到了示例代码,所以我不完全确定如何解决它。

d <- seq(from = 0.3, to = 0.5, by = 0.01)
nd <- length(d)


p <- seq(from = 0.6, to = 0.8, by = 0.1)
np <- length(p)


samplesizes <- data.frame(matrix(data=0, nrow=nd, ncol=np))
names(samplesizes) <- p
rownames(samplesizes) <- d
for (i in 1:np){
  for (j in 1:nd){
    result <- power.t.test(n=NULL, d = d[j], sig.level = .05,
                         power = p[i], alternative = "one.sided")
    samplesizes[j,i] <- result$n
  }
}
#-----------------------------------------------------    

xrange <- range(d)
yrange <- round(range(samplesizes))
colors <- rainbow(length(p))


plot(samplesizes, xrange, yrange, type="n", xlab="Sample size (n)",
     ylab="Effect Size (d)", las = 1)

for (i in 1:np){
  lines(s, effectsizes[,i], type="l", lwd=2, col=colors[i])
}
legend("topright", title="Power", as.character(p), fill=colors)

我正在尝试获得与此示例图类似的内容,但 Y 轴为样本,X 轴为效果大小。

【问题讨论】:

    标签: r matrix plot


    【解决方案1】:

    也许你正在寻找这个 -

    d <- seq(from = 0.3, to = 0.5, by = 0.01)
    nd <- length(d)
    
    
    p <- seq(from = 0.6, to = 0.8, by = 0.1)
    np <- length(p)
    
    
    samplesizes <- data.frame(matrix(data=0, nrow=nd, ncol=np))
    names(samplesizes) <- p
    rownames(samplesizes) <- d
    for (i in 1:np){
      for (j in 1:nd){
        result <- power.t.test(n=NULL, d = d[j], sig.level = .05,
                               power = p[i], alternative = "one.sided")
        samplesizes[j,i] <- result$n
      }
    }
    
    xrange <- range(d)
    yrange <- round(range(samplesizes))
    colors <- rainbow(length(p))
    
    
    plot(xrange, yrange, type="n", xlab="Sample size (n)", 
         ylab="Effect Size (d)", las = 1)
    
    for (i in 1:np){
      lines(d, samplesizes[,i], type="l", lwd=2, col=colors[i])
    }
    legend("topright", title="Power", as.character(p), fill=colors)
    

    【讨论】:

    • 啊,我明白了我的问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多