trying to display original and fitted data (nls + dnorm) with ggplot2's geom_smooth()

前端 未结 1 587
醉酒成梦
醉酒成梦 2021-02-01 09:28

I am exploring some data, so the first thing I wanted to do was try to fit a normal (Gaussian) distribution to it. This is my first time trying this in R, so I\'m taking it one

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 09:53

    the first error indicates that ggplot2 cannot find the variable 'count', which is used in formula, in data.

    Stats take place after mapping, that is, size -> x, and counts -> y.

    Here is an example for using nls in geom_smooth:

    ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() + 
      geom_smooth(method="nls", formula = y ~ N * dnorm(x, m, s), se=F, 
                  start=list(m=20, s=5, N=300)) 
    

    The point is that using x and y, instead of size and counts, in the specification of formula.

    0 讨论(0)
提交回复
热议问题