How to apply geom_smooth () for each group?

How to apply geom_smooth () for each group?

is used in the program facet_wrap(), so draw each group 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)
+4
source share
2 answers

You should 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)))

enter image description here

+4
source

Just add the mapping aes(group=Species)to the call geom_smooth().

The main plot:

  library(ggplot2); theme_set(theme_bw())
  g0 <- ggplot(data = iris, aes(x = Sepal.Length,  y = Petal.Length)) +
        geom_point(aes(color = Species))

geom_smooth:

  g0 + geom_smooth(aes(group=Species),
              method = "nls", formula = y ~ a * x + b, se = FALSE,
              method.args = list(start = list(a = 0.1, b = 0.1)))

If you add color matching (for a factor variable) that will have the same effect, plus the lines will be colored accordingly.

  g0 + geom_smooth(aes(colour=Species),
              method = "nls", formula = y ~ a * x + b, se = FALSE,
              method.args = list(start = list(a = 0.1, b = 0.1)))

@HubertL, , ggplot...

, , nls - geom_smooth(...,method="lm") ...

+1

Source: https://habr.com/ru/post/1660737/


All Articles