在 R 中使用 ggpubr 将帧和样本大小 (n) 添加到散点图

Add frame and sample size (n) to scatter plot using ggpubr in R

我在 R 中使用 ggpubr 包中的 ggscatter 函数。以 mtcars 数据集为例查看此代码。

library("ggpubr")
ggscatter(mtcars, x = "wt", y = "mpg", 
       add = "reg.line", conf.int = TRUE, 
       cor.coef = TRUE, cor.method = "pearson",
       xlab = "Weight (1000 lbs)", ylab = "Miles/(US) gallon")

现在,我想给情节添加一个框架,基本上是在顶部和底部添加另一个 x 轴和 y 轴(没有刻度或标签)。

最后,我想添加样本大小,以便顶部的标签显示:"R = -0.87, p = 1.3e-10, n = 32"。

有人可以帮我解决这个问题吗?谢谢。

不幸的是n的值我还没有找到一个优雅的方法来做,可能需要在功能上改变一些东西。

library(ggplot2)
library("ggpubr")
ggscatter(mtcars, x = "wt", y = "mpg", 
          add = "reg.line", conf.int = TRUE, 
          cor.coef = TRUE, cor.method = "pearson",
          xlab = "Weight (1000 lbs)", ylab = "Miles/(US) gallon", 
          cor.coef.coord = c(2.5, 30)) + # c(x, y)
  + annotate("text", x = 3.53, y = 30.1, label = paste0("(n = ", nrow(mtcars), ")")) # neilfws