我無法弄清楚如何使用 ggplot2 中的注釋寫出方程。 
這是我想要得到的一個例子,我只是不知道如何得到方程中的上標和 R^2。
這是我到目前為止的代碼:
df4 <- data.frame(hours=c(0,1,3,5,12,24,48,96,168,336,504,720), copies=c(603.3,406,588,393.27,458.47,501.67,767.53,444.13,340.6,298.47,61.42,51.6))
p1 <- ggplot(df4, aes(x=hours, y=copies)) geom_point() stat_smooth(method = 'nls', method.args = list(start = c(a=543.4172,b=-0.00247)), formula = y~a*exp(b*x), se=FALSE, linetype=2, colour="pink") theme_classic() xlab("") ylab("")
annotate("text", x = 400, y = 750, label = "N(t)=543.4172e^-0.00247259t\nR^2 = 0.6933", color = "black", hjust = 0, vjust = 1)
ggtitle(expression("eDNA pH 4"))
p1
我一直在看其他帖子,但似乎無法讓它發揮作用。
提前感謝您的任何幫助或建議!
uj5u.com熱心網友回復:
另一種選擇是使用parse=TRUEin annotate。此外,您的標簽需要一些修復才能使其成為有效的運算式。見?plotmath。
注意,TBMK 你不能在數學運算式中有任何換行符。annotates出于這個原因,我通過我設定的兩個添加了標簽的兩行,vjust以便將它們繪制在彼此之上:
df4 <- data.frame(hours = c(0, 1, 3, 5, 12, 24, 48, 96, 168, 336, 504, 720), copies = c(603.3, 406, 588, 393.27, 458.47, 501.67, 767.53, 444.13, 340.6, 298.47, 61.42, 51.6))
library(ggplot2)
p1 <- ggplot(df4, aes(x = hours, y = copies))
geom_point()
stat_smooth(
method = "nls", method.args = list(start = c(a = 543.4172, b = -0.00247)), formula = y ~ a * exp(b * x),
se = FALSE, linetype = 2, colour = "pink"
)
theme_classic()
xlab("")
ylab("")
annotate("text",
x = 400, y = 750, label = "K(t) == 543.4172^{-0.00247259*t}",
parse = TRUE,
color = "black", hjust = 0, vjust = -.1
)
annotate("text",
x = 400, y = 750, label = "R^2 == 0.6933",
parse = TRUE,
color = "black", hjust = 0, vjust = 1.1
)
ggtitle(expression("eDNA pH 4"))
p1

uj5u.com熱心網友回復:
我認為 bquote 是
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/417423.html
標籤:
