gpt4 book ai didi

r - 如何在散点图中显示相关系数?

转载 作者:行者123 更新时间:2023-12-01 23:07:48 26 4
gpt4 key购买 nike

在散点图中,我想沿着描述 x 和 y 之间关系的方程显示相关系数。我已经创建了我的数据 Material ,这是我目前的代码:

library(tidyverse)

# Creation of datamaterial

salary <- c(95, 100, 105, 110, 120, 124, 135, 150, 165, 175, 225, 230, 235, 260)
height <- c(160, 150, 182, 165, 172, 175, 183, 187, 174, 193, 201, 172, 180, 188)
fakenumbers <- data.frame(salary, height)

cor(height, salary, method = c("pearson"))

# Creation of scatterplot

r <- ggplot(fakenumbers, aes(x = height, y = salary)) +
geom_point(size = 3, shape = 21, color = "black", fill = "blue") +
labs(y = "Hourly salary
(sek)", x = "height (cm)", title = "Relationship between height and salary (made up data)") +
theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 18),
axis.title = element_text(size = 15),
axis.title.y = element_text(angle = 0, vjust = 0.5),
axis.text = element_text(size = 11))

# Adding a regressionline

r + geom_smooth(method = lm, formula = y ~ x, se = FALSE)

在坐标系内部,在回归线旁边,我想要显示“r = 0.588”和一些描述线性关系的方程式。我怎样才能做到这一点,最好使用 ggplot() 或其他一些函数?

最佳答案

我们可以使用 ggpubr 包,将 stat_cor(p.accuracy = 0.001, r.accuracy = 0.01) 添加到您的代码中:

library(ggpubr)
library(tidyverse)

r <- ggplot(fakenumbers, aes(x = height, y = salary)) +
geom_point(size = 3, shape = 21, color = "black", fill = "blue") +
stat_cor(p.accuracy = 0.001, r.accuracy = 0.01)+
labs(y = "Hourly salary
(sek)", x = "height (cm)", title = "Relationship between height and salary (made up data)") +
theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 18),
axis.title = element_text(size = 15),
axis.title.y = element_text(angle = 0, vjust = 0.5),
axis.text = element_text(size = 11))

enter image description here

关于r - 如何在散点图中显示相关系数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70494677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com