首页 > 解决方案 > 如何在 Facet 模式下定位 R 平方和方程?

问题描述

我想在刻面模式下显示每个图中的线性方程和 R 平方。到目前为止,这是我的代码。

library("ggplot2")
datos <- read.table("~/Documents/master2/plots/dosis_todos/datos.dat", header=TRUE, quote="\"")
ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +    
geom_point() + geom_smooth(method="lm", se=F) + 
facet_wrap(~datos$cristal)

在此答案中阅读了有关 ggpmisc 的信息后,我尝试了

my.formula <- y ~ x
library("ggpmisc")
ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +    
geom_point() + 
geom_smooth(method="lm", se=F, formula=my.formula) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), formula = my.formula, parse = TRUE) +
facet_wrap(~datos$cristal)

哪种方法有效,除了方程的位置对于每个情节都会下降,直到消失......

在此处输入图像描述

如果我将我的情节保存得足够大,我可以在 9 个情节中看到我的所有文本......下降。

所以我想问题是如何保持方程的位置和R平方信息的固定?

谢谢

附言。是的,我知道 N57 只有 3 分 :(

附言。这是我的数据的链接

标签: rggplot2ggpmisc

解决方案


@murpholinox 是的,你是对的,'ggpmisc' 中的代码还不够聪明,无法检测到每个面板的不同颜色等美学值何时是唯一的。但是,可以手动定位方程,将数据单元中的位置传递给参数label.y和/或label.x。所以,有一个解决方法。

library("ggplot2")
library("ggpmisc")
datos <- read.table("datos.dat", header=TRUE, quote="\"")
my.formula <- y ~ x
ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +
geom_point() +
geom_smooth(method="lm", se=F, formula=my.formula) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
             formula = my.formula, parse = TRUE, label.y = 0.9) +
ylim(0, 1) +
facet_wrap(~datos$cristal)

阴谋

也可以将向量传递给label.ylabel.x,以便可以为每个面板手动定位每个方程。

ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +
geom_point() +
geom_smooth(method="lm", se=F, formula=my.formula) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
             formula = my.formula, parse = TRUE, 
             label.y = c(rep(0.9, 6), rep(0.15, 2), 0.9)) +
ylim(0, 0.95) +
facet_wrap(~datos$cristal)

在此处输入图像描述


推荐阅读