修改stat_cor函数以输出“ rho”而不是“ R”

时间:2019-10-31 17:38:20

标签: r

是否可以将stat_cor(ggpubr软件包)功能的输出表达式从“ R”更改为“ rho”?这是一个使用“ R”表达式生成图的简单示例:

library(ggplot2)
library(ggpubr)

data(mtcars)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p + geom_smooth(method = "lm", se = FALSE)+
  stat_cor(method="pearson", aes(wt, mpg), label.x = 3, 
           label.x.npc = "right", label.y.npc = "top")

我尝试了trace(stat_cor, edit=TRUE)选项, 得到了以下代码:

function (mapping = NULL, data = NULL, method = "pearson", label.sep = ", ", 
  label.x.npc = "left", label.y.npc = "top", label.x = NULL, 
  label.y = NULL, output.type = "expression", geom = "text", 
  position = "identity", na.rm = FALSE, show.legend = NA, 
  inherit.aes = TRUE, ...) 
{
  parse <- ifelse(output.type == "expression", TRUE, FALSE)
  layer(stat = StatCor, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
    params = list(label.x.npc = label.x.npc, label.y.npc = label.y.npc, 
      label.x = label.x, label.y = label.y, label.sep = label.sep, 
      method = method, output.type = output.type, parse = parse, 
      na.rm = na.rm, ...))
}

但是我无法弄清楚哪些行需要修改。 在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

标签在源文件ggpubr/R/stat_cor.R中的功能.cor_test中定义。所以跑

trace(ggpubr:::.cor_test, edit=TRUE)

并编辑italic(R)成为rho

if (output.type == "expression") {
    z <- z %>% dplyr::mutate(r.label = paste("rho", r, sep = "~`=`~"), 

保存后,运行代码并获得下图。

enter image description here

答案 1 :(得分:0)

您可以破解.cor_test函数@RuiBarradas所指,或者稍微尝试一下(下面的复杂方法):

param_2

我基本上用“ R =“代替了“ Rho =”。

enter image description here