Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less smoothed line in ggplot2, alternatives to geom_smooth? [duplicate]

Using the following data:

> str(attribute)
'data.frame':   431 obs. of  2 variables:
 $ pos: int  1 2 3 4 5 6 7 8 9 10 ...
 $ att: num  0.652 0.733 0.815 1.079 0.885 ...   *[between 0 and 3]

and:

ggplot(attribute, aes(x=pos, y=att)) + geom_line() + geom_smooth()

I did: enter image description here

I would like to progressively smooth the black curve, not "as much as" geom_smooth default did. I've tried n, level options, but didn't do what I want. Which would be the best way to increase smoothing progressively? (e.g. average 2 values in one, then try 3 in one, and so on). I guess it's something really easy or achievable without using geom_smooth, but I don't know what to search/look for. Thanks.

like image 268
PGreen Avatar asked Mar 13 '15 17:03

PGreen


People also ask

How do I smooth out a line graph in ggplot2?

stat_smooth() and geom_smooth() both are aliases. Both of them have the same arguments and both of them are used to plot a smooth line. We can plot a smooth line using the “loess” method of stat_smooth() function.

What is the difference between Stat_smooth and Geom_smooth?

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.

What is the difference between Geom_line and Geom_smooth?

Geom_line creates a single line for both panels and distributes the colors according to the colour variable, while geom_smooth does not draw the smooth line in the 2nd panel.

What is the default method for fitting a best fit line with Geom_smooth?

By default, geom_smooth will inherit the dataset that you specify with the top-line call to ggplot() .


1 Answers

This is documented in stat_smooth. The default smoother is loess, and additional arguments are passed on to it, as specified for the description of the ... argument. So what you want is span:

ggplot(mtcars,aes(x = wt,y = mpg)) + 
  geom_point() + 
  geom_smooth(span = 0.4)

Additionally, loess accepts a degree argument for more control over the amount of smoothing.

like image 80
joran Avatar answered Oct 17 '22 14:10

joran