Plot a smooth graph in R

In RI want to Plot a smooth graph (with its shadowed margin) which contains “age” and “circumference” variables on X and Y-axis for ready Orange data

What is “method” of smoothing which ggplot2 uses?

I do like this but get erros:

x <- Orange[2]
y <- Orange[3]
lo <- loess(y~x)
plot(x,y)
lines(predict(lo), col='red', lwd=2)

Error(s), warning(s): Error in model.frame.default(formula = y ~ x) : invalid type (list) for variable 'y' Calls: loess -> eval -> eval -> -> model.frame.default Execution halted


From ?geom_smooth: "for [the default] the smoothing method is chosen based on the size of the largest group (across all panels). 'loess()' is used for less than 1,000 observations; otherwise 'mgcv::gam()' is used"

So for Orange you will get loess anyway, but you can be explicit by setting the method.

ggplot(Orange,aes(x=age,y=circumference))+geom_point()+geom_smooth(method="loess")

You can use geom_smooth(). Example as below.

library(ggplot2)
ggplot(diamonds,aes(x=carat,y=price)) + 
  geom_point(aes(color=cut))+
  geom_smooth()
链接地址: http://www.djcxy.com/p/30924.html

上一篇: 平行坐标

下一篇: 在R中画一个平滑的图