只是想在這里澄清一下,如果有人有一個不同的例子。最初我想使用這個例子,因為它有圖表、平均線和 r 值都顯示在圖表中:
uj5u.com熱心網友回復:
您可以使用geom_smooth函式ggplot2并實作相關系數以及 p 值,如下所示:
library(ggplot2)
my_data <- mtcars
cor_coefs <- cor.test(my_data$mpg, my_data$wt)
ggplot(data = my_data, aes(x = mpg, y = wt))
geom_point()
geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE)
annotate("text", x = 30, y = 4, label = paste0("R: ", round(cor_coefs$estimate, 2)))
annotate("text", x = 30, y = 3.5, label = paste0("p-value: ", round(cor_coefs$p.value, 10)))
cor_coefs保護相關性測驗,您可以使用它來獲得所需的值。使用annotatefrom ggplot2,您需要指定文本的 x 和 y 位置。您可以根據您的需要動態地實作它(因為您沒有提供任何資料)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409174.html
標籤:
上一篇:將ggplot變成函式
下一篇:在繪圖周圍創建多邊形
