首页 > 解决方案 > 如何使用绘图功能从矩阵中绘制多列

问题描述

我正在尝试使用 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 轴上的效果大小。

在此处输入图像描述

标签: rmatrixplot

解决方案


也许你正在寻找这个 -

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)

在此处输入图像描述


推荐阅读