我是R的初学者,我试图在一个情节中放置包含上标变量的等式。我知道如何在图上打印上标字母,但我无法找到插入变量的方法。这是我的代码:
DF <- data.frame(X <- c(1, 2, 3, 4, 5, 6, 7), Y <- c(0, 0, 1, 0, 1, 1, 1))
# Logistic regression
model <- glm(Y ~ X, family = binomial, data = DF)
# Plot raw data
raw_plot <- plot(DF$X, DF$Y,
xlab = 'X', ylab = 'Y'
)
# Add prediction curve
curve(predict(model, data.frame(X = x), type = 'response'), add = TRUE)
# Get coefficients
intercept <- summary(model)$coefficients[1] # -4.361418
coefficient <- summary(model)$coefficients[2] # 1.250679
superscript.part <- sprintf('%.2f + %.2f*x', intercept, coefficient)
text(5, 0.2, labels = expression(paste('y = 1/(1 + 1/e'^'superscript.part'*')')))
# This will superscript 'superscript' and not the actual variable
这就是我得到的。
有没有办法真正使用上标打印变量的内容?谢谢你的帮助!
答案 0 :(得分:2)
这有效:
text(5,.2, bquote("y = 1/(1 + 1/e" ^~{.(superscript.part)} ~ ")"))