相关疑难解决方法(0)

在图上添加回归线方程和R2

我想知道如何添加回归线方程和R ^ 2 ggplot.我的代码是

library(ggplot2)

df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
p <- ggplot(data = df, aes(x = x, y = y)) +
            geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
            geom_point()
p
Run Code Online (Sandbox Code Playgroud)

任何帮助将受到高度赞赏.

r ggplot2 ggpmisc

207
推荐指数
8
解决办法
23万
查看次数

ggplot2:在图中添加p值

我有这个情节

在此输入图像描述

使用下面的代码

library(dplyr) 
library(ggplot2)
library(ggpmisc)

df <- diamonds %>%
  dplyr::filter(cut%in%c("Fair","Ideal")) %>%
  dplyr::filter(clarity%in%c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1",  "VVS2")) %>%
  dplyr::mutate(new_price = ifelse(cut == "Fair", 
                                   price* 0.5, 
                                   price * 1.1))

formula <- y ~ x    
ggplot(df, aes(x= new_price, y= carat, color = cut)) +
  geom_point(alpha = 0.3) +
  facet_wrap(~clarity, scales = "free_y") +
  geom_smooth(method = "lm", formula = formula, se = F) +
  stat_poly_eq(aes(label = paste(..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.15,
               formula = formula, parse = TRUE, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 p-value

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

ggplot2 ×2

r ×2

ggpmisc ×1

p-value ×1