How can I apply geom_smooth()
for every group ?
The code below uses facet_wrap()
, so plots every group in a separate graph.
I would like to integrate the graph, and get one graph.
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(aes(color = Species)) +
geom_smooth(method = "nls", formula = y ~ a * x + b, se = F,
method.args = list(start = list(a = 0.1, b = 0.1))) +
facet_wrap(~ Species)
The warning geom_smooth() using formula 'y ~ x' is not an error. Since you did not supply a formula for the fit, geom_smooth assumed y ~ x, which is just a linear relationship between x and y. You can avoid this warning by using geom_smooth(formula = y ~ x, method = "lm")
It then computes the confidence interval based on a set number of standard-error intervals around the predicted value (for the typical 95% CI, this is predicted ± 1.96 * se ). The Details section of geom_smooth says: Calculation is performed by the (currently undocumented) 'predictdf()' generic and its methods.
geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.
By default, the loess or gam function is used for smoothing (in relation to the size of dataset). Using the method combo-box, you can change this function to lm, glm, gam, loess, rlm.
You have to put all your variable in ggplot aes()
:
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point() +
geom_smooth(method = "nls", formula = y ~ a * x + b, se = F,
method.args = list(start = list(a = 0.1, b = 0.1)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With