1

I have a data that looks like this:

>data(growth)
Year  Growth
<int> <dbl>
1  1952 0.947
2  1953 1.15 
3  1954 1.09 
4  1955 1.12 
5  1956 0.869
6  1957 1.12 
7  1958 1.22 
8  1959 0.973
9  1960 0.901
10  1961 1.03 

I want to create a time series and then map a 'trend smoothing spline' on the plot. I know the code for the spline is

p + stat_smooth(
color = "#FC4E07", fill = "#FC4E07",
method = "loess")

However, I don't know how can I extract that spline (without the background time series) from the plot, and then combine 4 splines into one graph?

Thanks a lot.

  • 1
    Welcome to Stack Overflow! Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](https://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Ralf Stubner Jun 28 '18 at 20:33
  • 1
    Related: [Method to extract stat_smooth line fit](https://stackoverflow.com/questions/9789871/method-to-extract-stat-smooth-line-fit) – markus Jun 28 '18 at 21:15
  • I would recommend you have a look at the last answer by @phillyooo in the link posted by @markus. The way that provides you with highest flexibility for plotting is to fit one or several models (e.g. `loess`, `gam`, etc.) on your data in a separate step before plotting. This way you can use `predict` for each model of interest to generate data for plotting over a specified range. Then you can use these data points in ggplot with `geom_point`, `geom_line`, or whatever is suitable. I hope this short summarizing repetition of the linked answer helps. – Manuel Bickel Jun 28 '18 at 22:04
  • Maybe stack your datasets together (with a column to represent the dataset name) and make a single plot with a separate smooth by group? That can be done via `group` (or other aesthetics like `color`) in `aes`. – aosmith Jun 29 '18 at 00:29

0 Answers0