简体   繁体   English

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

[英]Using custom functions using geom_smooth and faceting in ggplot

I'm currently using facet_grid to display scatterplots for each participant and task.我目前正在使用 facet_grid 显示每个参与者和任务的散点图。 However, I also want to overlay a custom function fit to each participant and task.但是,我还想覆盖适合每个参与者和任务的自定义 function。 The data structure looks like this:数据结构如下所示:

participant参与者 task任务 ratio比率 resp响应 uni_beta uni_beta
sub1子1 task1任务1 0.25 0.25 0.28 0.28 0.95 0.95
sub1子1 task1任务1 0.5 0.5 0.51 0.51 0.95 0.95
... ... ... ... ... ... ... ... ... ...

The variable uni_beta is the coefficient obtained when fitting the cpf function for that specific task and participant变量 uni_beta 是为特定任务和参与者拟合 cpf function 时获得的系数

I've tried this and many other versions of it, but I still can't get the curve to print over the scatter plot.我已经尝试过这个和许多其他版本,但我仍然无法在散点 plot 上打印曲线。

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 outputs all the facetted scatterplots, but the cpf function curve does not appear over the scatterplots. R 输出所有分面散点图,但 cpf function 曲线不会出现在散点图上。 I've made sure that my variables ratio, resp and uni_beta are all numeric.我确保我的变量 ratio、resp 和 uni_beta 都是数字的。

I tried following this solution as well(still only get plots and no line): Custom lm formula in geom_smooth我也尝试遵循此解决方案(仍然只得到图而没有线): Geom_smooth 中的自定义 lm 公式

This is the code I tried using this solution:这是我尝试使用此解决方案的代码:

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

p <- ggplot(df_power, aes(x = ratio, 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

Any help would be appreciated任何帮助,将不胜感激

Data(following edit):数据(以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 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Audi_ratio", "Line_ratio"), class = "factor"), 
    ratio = c(0.75, 0.583, 0.25, 0.583, 0.417, 0.25, 0.083, 0.333, 
    0.583, 0.667, 0.417, 0.5, 0.333, 0.083, 0.833, 0.5, 0.917, 
    0.75, 0.167, 0.75, 0.667, 0.167, 0.917, 0.667, 0.333, 0.833, 
    0.917, 0.25, 0.417, 0.5, 0.167, 0.083, 0.833, 0.583, 0.5, 
    0.333, 0.25, 0.833, 0.667, 0.917, 0.083, 0.083, 0.917, 0.833, 
    0.417, 0.75, 0.083, 0.667, 0.417, 0.583, 0.167, 0.167, 0.5, 
    0.75, 0.667, 0.833, 0.333, 0.583, 0.75, 0.5, 0.917, 0.167, 
    0.417, 0.25, 0.25, 0.333, 0.5, 0.25, 0.417, 0.917, 0.25, 
    0.667, 0.583, 0.417, 0.083, 0.083, 0.75, 0.75, 0.333, 0.917, 
    0.583, 0.833, 0.917, 0.583, 0.5, 0.333, 0.25, 0.417, 0.167, 
    0.167, 0.833, 0.167, 0.75, 0.083, 0.833, 0.333, 0.667, 0.667, 
    0.5, 0.917, 0.25, 0.417, 0.833, 0.167, 0.75, 0.083, 0.583, 
    0.167, 0.667, 0.333, 0.5, 0.167, 0.667, 0.75, 0.25, 0.5, 
    0.417, 0.417, 0.583, 0.5, 0.583, 0.083, 0.75, 0.917, 0.667, 
    0.083, 0.333, 0.333, 0.917, 0.25, 0.833, 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.390598291, 0.5, 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.344444444, 0.258974359, 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.251412796, 
    0.698018472, 0.26943724, 0.802159705, 0.337529585, 0.688004891, 
    0.657964151, 0.497746868, 0.896287359, 0.203347611, 0.441670819, 
    0.826192298, 0.155282426, 0.720048348, 0.043130328, 0.619912546, 
    0.13325255, 0.553822917, 0.379586622, 0.557828349, 0.181317735, 
    0.633931558, 0.792146125, 0.353551314, 0.39560835, 0.413632795, 
    0.411630079, 0.585866374, 0.49173872, 0.7140402, 0.043130328, 
    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, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    0.509969418967693, 0.509969418967693, 0.509969418967693, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 1.05982442358554, 1.05982442358554, 1.05982442358554, 
    1.05982442358554, 0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.722168662950021, 0.722168662950021, 0.722168662950021, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383, 
    0.988198700734383, 0.988198700734383, 0.988198700734383)), row.names = c(NA, 
-132L), class = "data.frame")

I found the problem.我发现了问题。 It works when I use the nls method.当我使用 nls 方法时它可以工作。 So:所以:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM