在ggplot中使用geom_smooth和faceting使用自定义函数

问题描述

我目前正在使用 facet_grid 来显示每个参与者和任务的散点图。但是,我还想覆盖适合每个参与者和任务的自定义函数。 数据结构如下:

参与者 任务 比率 响应 uni_beta
sub1 任务 1 0.25 0.28 0.95
sub1 任务 1 0.5 0.51 0.95
... ... ... ... ...

变量 uni_beta 是为该特定任务和参与者拟合 cpf 函数时获得的系数

我已经尝试过这个和它的许多其他版本,但我仍然无法将曲线打印在散点图上。

cpf <-function(ratio,beta){return(resp~(ratio^beta)/((ratio^beta)+(1-ratio)^beta))}

ggplot(df_power,aes(x = ratio,y = resp)) +
  geom_point() +
  facet_grid(participant~task)+
  stat_smooth(method = "lm",formula = cpf(x,uni_beta))

R 输出所有分面散点图,但 cpf 函数曲线没有出现在散点图上。我已经确保我的变量 ratio、resp 和 uni_beta 都是数字。

我也尝试遵循此解决方案(仍然只得到图而没有线):Custom lm formula in geom_smooth

这是我尝试使用此解决方案的代码

cpf <-function(df){return(df$resp~(df$ratio^df$beta)/((df$ratio^df$beta)+(1-df$ratio)^df$beta))}

p <- ggplot(df_power,y = resp)) +
  geom_point() +
  facet_wrap(participant~.,ncol = 1)

p_smooth <- by(df_power,df_power$participant,function(x) geom_smooth(data=x,method = lm,formula = cpf(x)))

p+p_smooth

任何帮助将不胜感激

数据(以下编辑):

structure(list(participant = structure(c(1L,1L,2L,2L),.Label = c("001","002"),class = "factor"),task = structure(c(1L,2L
),.Label = c("Audi_ratio","Line_ratio"),ratio = c(0.75,0.583,0.25,0.417,0.083,0.333,0.667,0.5,0.833,0.917,0.75,0.167,0.833),resp = c(0.746153846,0.317094017,0.284615385,0.611111111,0.447008547,0.361538462,0.327350427,0.390598291,0.631623932,0.54957265,0.561538462,0.470940171,0.388888889,0.127350427,0.607692308,0.525641026,0.835042735,0.669230769,0.575213675,0.643589744,0.55982906,0.24017094,0.730769231,0.493162393,0.445299145,0.626495726,0.751282051,0.281196581,0.21965812,0.118803419,0.698290598,0.628205128,0.503418803,0.282905983,0.216239316,0.91025641,0.597435897,0.968376068,0.048717949,0.021367521,0.976923077,0.951282051,0.409401709,0.874358974,0.064102564,0.588888889,0.421367521,0.585470085,0.158119658,0.152991453,0.337606838,0.682905983,0.641880342,0.833333333,0.258974359,0.344444444,0.670940171,0.541025641,0.845299145,0.202564103,0.236752137,0.322222222,0.48172514,0.195336747,0.397611066,0.762105385,0.359559462,0.413632795,0.497746868,0.327516005,0.079179217,0.019097736,0.784135261,0.72405378,0.321507857,0.770116249,0.645947855,0.814176002,0.878262915,0.51176588,0.507760448,0.445676251,0.343537733,0.553822917,0.251412796,0.2894644,0.70402662,0.698018472,0.26943724,0.802159705,0.337529585,0.688004891,0.657964151,0.896287359,0.203347611,0.441670819,0.826192298,0.155282426,0.720048348,0.043130328,0.619912546,0.13325255,0.379586622,0.557828349,0.181317735,0.633931558,0.792146125,0.353551314,0.39560835,0.411630079,0.585866374,0.49173872,0.7140402,0.61790983,0.922322667,0.808167853,0.099206377,0.479722424,0.325513289,0.908303655,0.211358475,0.918317235,0.842214026
    ),uni_beta = c(0.509969418967693,0.509969418967693,1.05982442358554,0.722168662950021,0.988198700734383,0.988198700734383)),row.names = c(NA,-132L),class = "data.frame")

解决方法

我发现了问题。当我使用 nls 方法时它起作用。所以:

ggplot(df,aes(x = ratio,y = resp)) + geom_point() +
  facet_grid(participant~task) + 
  geom_smooth(method = "nls",formula = y ~ x^b/(x^b +(1-x)^b),method.args = list(start=c(b=0.5)),# 
              se = F)