是否可以将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, ...))
}
但是我无法弄清楚哪些行需要修改。 在此先感谢您的帮助。
答案 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 = "~`=`~"),
保存后,运行代码并获得下图。
答案 1 :(得分:0)