使用自定义函数进行平滑化

huangapple go评论100阅读模式
英文:

using a custom function for smoothing

问题

我最近开始学习R,并在绘图时使用自定义函数时遇到了问题。
我想要使用罗森-拉姆勒方程,但不知道如何在我的情况下使用它。在Mathkad中,这很容易解决。但在这里,我无法理解。

这是我的数据和代码示例:

  1. ggplot(allOnw, aes(x = xy, y=yy, col = sem1)) + geom_point() +
  2. guides(fill = FALSE) + labs(col="S") + theme(legend.position="bottom") +
  3. scale_x_continuous(breaks = seq(from = 0, to = 160, by = 10), limits = c(0,160)) +
  4. scale_color_manual(values = c("blue", "cyan", "darkgreen", "darkorange", "gold", "darkorchid")) +
  5. facet_wrap(vars(cat1), ncol = 1, strip.position = "right") +
  6. labs(x="MP") + labs(y="GT")

我希望图形看起来像使用geom_density时的效果。我能做些什么?这可能吗?

英文:

I recently started learning R and ran into a problem using a custom function when plotting.
I want to use the rosin-rammler equation but can't figure out how to use it in my case. In Mathkad, this was easily solved. And here I can not understand.

This is an example of my data and code.

xy yy cat1 sem1
0 0 0 №1
10 3 0 №1
20 30 0 №1
30 18 0 №1
40 15 0 №1
50 13 0 №1
60 8 0 №1
70 5 0 №1
80 3 0 №1
90 2 0 №1
100 0 0 №1
110 0 0 №1
  1. ggplot(allOnw, aes(x = xy, y=yy, col = sem1)) +geom_point()+
  2. guides(fill = FALSE) + labs(col="S" )+theme(legend.position="bottom") +scale_x_continuous(breaks = seq(from = 0, to = 160, by = 10), limits = c(0,160))+
  3. scale_color_manual(values = c("blue", "cyan", "darkgreen","darkorange","gold","darkorchid")) +
  4. facet_wrap(vars(cat1),ncol = 1, strip.position = "right")+labs(x ="MP")+labs(y = "GT ")

I would like the graph to end up looking like when using geom_density.
What can i do for this?
Is it possible?

答案1

得分: 0

如果您要拟合特定模型到您的数据点,您可以在geom_smooth内部使用method = nls

据我从文献了解,您尝试拟合的函数具有以下形式:

  1. rosin_rammler <- function(x, A, B, C) {
  2. C * ((A / x) * (x / B)^A * exp(-((x / B)^(A - 1)))
  3. }

我们可以直接在geom_smooth内使用这个函数:

  1. ggplot(allOnw, aes(x = xy + 1e-6, y = yy, col = sem1)) +
  2. geom_point() +
  3. geom_smooth(formula = y ~ rosin_rammler(x, A, B, C), method = nls,
  4. method.args = list(algorithm = "port",
  5. start = list(A = 5, B = 20, C = 1000),
  6. lower = c(1, 1)), se = FALSE, n = 1000) +
  7. labs(col = "S", x = "MP", y = "GT")+
  8. theme(legend.position = "bottom") +
  9. scale_x_continuous(breaks = 0:16 * 10, limits = c(0, 160)) +
  10. scale_color_manual(values = c("blue", "cyan", "darkgreen",
  11. "darkorange", "gold", "darkorchid")) +
  12. facet_wrap(vars(cat1), ncol = 1, strip.position = "right")

使用自定义函数进行平滑化

英文:

If you have a particular model to fit to your points, you can use method = nls inside geom_smooth.

As far as I can tell from the literature, the function you are trying to fit takes this form:

  1. rosin_rammler &lt;- function(x, A, B, C) {
  2. C * ((A / x) * (x / B)^A * exp(-((x / B)^(A - 1))))
  3. }

And we can use this directly inside geom_smooth:

  1. ggplot(allOnw, aes(x = xy + 1e-6, y = yy, col = sem1)) +
  2. geom_point() +
  3. geom_smooth(formula = y ~ rosin_rammler(x, A, B, C), method = nls,
  4. method.args = list(algorithm = &quot;port&quot;,
  5. start = list(A = 5, B = 20, C = 1000),
  6. lower = c(1, 1)), se = FALSE, n = 1000) +
  7. labs(col = &quot;S&quot;, x = &quot;MP&quot;, y = &quot;GT&quot;)+
  8. theme(legend.position = &quot;bottom&quot;) +
  9. scale_x_continuous(breaks = 0:16 * 10, limits = c(0, 160)) +
  10. scale_color_manual(values = c(&quot;blue&quot;, &quot;cyan&quot;, &quot;darkgreen&quot;,
  11. &quot;darkorange&quot;, &quot;gold&quot;, &quot;darkorchid&quot;)) +
  12. facet_wrap(vars(cat1), ncol = 1, strip.position = &quot;right&quot;)

使用自定义函数进行平滑化

huangapple
  • 本文由 发表于 2023年2月14日 03:17:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定