如何在刻面模式中定位R平方和方程?

如何在刻面模式中定位R平方和方程?,r,ggplot2,ggpmisc,R,Ggplot2,Ggpmisc,我想在刻面模式下显示每个图中的线性方程和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(

我想在刻面模式下显示每个图中的线性方程和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)
库(“ggplot2”)

datos多亏@aosmith的评论,我可以做我想做的事。

代码是:

ggplot(datos, aes(corriente, dosis)) +
     geom_point(shape = 21, size = 3) +
     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)

@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)

请使用
dput()
(不是
str
head
或图片/屏幕截图)共享您的数据样本,以便其他人可以提供帮助。在阅读的基础上看更多,我认为这是因为你给方程上色,所以它们是堆叠的(在下面的例子中有一个例子)。您可以删除全局
color
映射并将其放在其他层中,或者使用类似
color=black
的内容来覆盖该美学效果。我在inkscape中手动调整方程式和R平方:钉住我看到的所有关于stat\u poly\u eq的帖子,每个人都设置
sep=“~~”
来增加一些空间。如果我想让R^2出现在下一行,sep参数会是什么样子?我的图形,如上面的示例,没有足够的水平空间容纳同一行上的等式和R^2,除非我真的缩小文本大小。据我所知,在R表达式中不可能出现换行。在这种情况下,最好的选择是添加两个层,一个带等式,一个带R2。我的意思是添加stat\u poly\u eq()两次。另一个选项是映射大小,比如说
size=2.7
@PedroAphalo谢谢。按照你的建议,我可以通过添加两条stat_poly_eq()线来获得我想要的结果。另外,我还可以单独操纵方程式和R2的外观@佩德罗法罗:另一个问题,你知道如何迫使回归线穿过原点吗。我尝试了
OriginFormula
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)
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)