R、 尝试向绘图添加文本时未调用plot.new

R、 尝试向绘图添加文本时未调用plot.new,r,R,我三天前开始学习R代码,现在终于到了我被卡住的地步。运行脚本时会出现错误消息 Error in text.default(x = (x - 2 * sigma), y = 0.1, label = "Z =", z) : plot.new has not been called yet 我尝试过将它放置在不同的代码行中,即使是在ggplot命令中,每次都是同一个问题,我完全不知道如何修复它,其他一切都很好,我已经在下面包含了完整的代码,文本命令位于底部,非常感谢您的帮助,谢谢您的时间 lib

我三天前开始学习R代码,现在终于到了我被卡住的地步。运行脚本时会出现错误消息

Error in text.default(x = (x - 2 * sigma), y = 0.1, label = "Z =", z) : 
plot.new has not been called yet
我尝试过将它放置在不同的代码行中,即使是在ggplot命令中,每次都是同一个问题,我完全不知道如何修复它,其他一切都很好,我已经在下面包含了完整的代码,文本命令位于底部,非常感谢您的帮助,谢谢您的时间

library(cowplot)
print("This is a normal distribution for a sample mean for upper tail test")

x <- 130  # mean
sigma <- 30  #standard deviation
x_bar <- 150   #new mean for the hypothesis
n <- 2  #number of trials
z <- (x_bar - x) / (sigma / sqrt(n))
sig_level <- 0.05  #put at %/100 e.g. 50%/100 = 0.5

p1 <- ggplot(data = data.frame(x = c(x - sigma*3, x + sigma*3)), aes(x))
p1 <- p1 +  stat_function(fun = dnorm, n = 101, args = list(mean = x, sd = 
sigma)) + ylab("")
p1 <- p1 +  scale_y_continuous(breaks = NULL)
p1 <- p1 + geom_vline(xintercept = range(x + sigma*3, x - sigma*3), color = 
'red3', size = 0.5)
p1 <- p1 + geom_vline(xintercept = range(x + sigma, x - sigma), color = 'blue3', 
size = 0.5)
p1 <- p1 + geom_vline(xintercept = range(x + sigma*2, x - sigma*2), color = 
'green', size = 0.5)
p1 <- p1 + geom_vline(xintercept = x, color='black', size = 0.5)


if (x_bar != -123456789) {
  p1 <- p1 + geom_vline(xintercept = x_bar, color = 'darkmagenta', size = 1)
}

df <- data.frame(x, sigma, x_bar, n, z)
df
cat("the probability that the new mean =", x_bar,"is", 1-pnorm(z))

if (sig_level > 1-pnorm(z) | sig_level > pnorm(z)) {
  cat("Your new mean is in the critical region (", sig_level,") and is therefore unlikely")
}

text( x = (x - 2 * sigma), y = 0.1, label = "Z =", z)

plot(p1)
库(cowplot)
打印(“这是上尾测试样本平均值的正态分布”)
xtext()函数与R中的base plot函数一起使用。这就是出现此错误的原因

根据您的代码:您正在使用ggplot绘图功能。因此,您可以使用annotate()函数实现相同的功能

p1 + annotate("text", x=(x - 2 * sigma), y= 0.1, label=paste("z = ",z)) 
运行代码为:

x <- 130  # mean
sigma <- 30  #standard deviation
x_bar <- 150   #new mean for the hypothesis
n <- 2  #number of trials
z <- (x_bar - x) / (sigma / sqrt(n))
sig_level <- 0.05  #put at %/100 e.g. 50%/100 = 0.5

p1 <- ggplot(data = data.frame(x = c(x - sigma*3, x + sigma*3)), aes(x))
p1 <- p1 +  stat_function(fun = dnorm, n = 101, args = list(mean = x, sd = 
sigma)) + ylab("")
p1 <- p1 +  scale_y_continuous(breaks = NULL)
p1 <- p1 + geom_vline(xintercept = range(x + sigma*3, x - sigma*3), color = 
'red3', size = 0.5)
p1 <- p1 + geom_vline(xintercept = range(x + sigma, x - sigma), color = 'blue3', 
size = 0.5)
p1 <- p1 + geom_vline(xintercept = range(x + sigma*2, x - sigma*2), color = 
'green', size = 0.5)
p1 <- p1 + geom_vline(xintercept = x, color='black', size = 0.5)


if (x_bar != -123456789) {
  p1 <- p1 + geom_vline(xintercept = x_bar, color = 'darkmagenta', size = 1)
}

df <- data.frame(x, sigma, x_bar, n, z)
df
cat("the probability that the new mean =", x_bar,"is", 1-pnorm(z))

if (sig_level > 1-pnorm(z) | sig_level > pnorm(z)) {
  cat("Your new mean is in the critical region (", sig_level,") and is therefore unlikely")
}

p1 + annotate("text", x=(x - 2 * sigma), y= 0.1, label=paste("z = ",z))

x使用
p1上面的注释是另一种方法来做同样的事情!