ggplot2:添加多列的p值,Rsq和斜率

时间:2018-11-02 15:47:58

标签: r ggplot2 lm p-value

假设我有这个数据框:

library(ggplot2)
Y <- rnorm(100)
df <- data.frame(A = rnorm(100), B = runif(100), C = rlnorm(100),
                 Y = Y)
colNames <- names(df)[1:3]
for(i in colNames){
  plt <- ggplot(df, aes_string(x=i, y = Y)) +
    geom_point(color="#B20000", size=4, alpha=0.5) +
    geom_hline(yintercept=0, size=0.06, color="black") + 
    geom_smooth(method=lm, alpha=0.25, color="black", fill="black")
  print(plt)
  Sys.sleep(2)
}

我想做一个lm模型,并为每列显示调整后的Rsq,截距,斜率和p值。我在下面找到了一个例子

data(iris)
ggplotRegression <- function (fit) {

require(ggplot2)

ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red") +
  labs(title = paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 5),
                     "Intercept =",signif(fit$coef[[1]],5 ),
                     " Slope =",signif(fit$coef[[2]], 5),
                     " P =",signif(summary(fit)$coef[2,4], 5)))
}

fit1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
ggplotRegression(fit1)

但是它仅适用于一列。 (我摘自this questionthis one over here的示例)

谢谢!

1 个答案:

答案 0 :(得分:1)

基于上面的注释,您可以将fit放入函数中,然后使用<mat-card class="example-card"> <mat-card-header> <mat-card-title>Your Chats</mat-card-title> </mat-card-header> <mat-card-content> <mat-selection-list #chats> <h3 mat-subheader>Chats</h3> <mat-list-option *ngFor="let chat of (chats | async)" (click)="handleClick(chats)" [value]="chat.id"> <img matListAvatar src="./assets/avatar.svg" alt="Avatar Icon"> <h3 mat-line>{{chat.name}}</h3> <p mat-line>test</p> </mat-list-option> </mat-selection-list> </mat-card-content> </mat-card> 进行遍历。

lapply

enter image description here