7

I would like to mimic Excel smoothing style in R using ggplot2 and plotly.

Packages

library(dplyr)
library(tibble)
library(ggplot2)
library(plotly)
library(ggforce) #only for geom_bspline() in example below (other smoothing functions can be used)
library(scales) #only for percent() formatting below 

Example dataset

df <- tibble(Group = rep(LETTERS[1:2], each = 10),
             x = rep(1:10, 2),
             y = c(2, 5, 9, 15, 19, 19, 15, 9, 5, 2,
                   1, 0, 3, 2, 3, 4, 14, 24, 24, 25)*0.01)

What I want

enter image description here

What I have so far

(df %>%
    ggplot(aes(x, y)) +
    geom_point(aes(color = Group)) +
    ggforce::geom_bspline(aes(group = Group, color = Group)) +
    scale_y_continuous(limits = c(0, 0.35), labels = scales::percent) +
    scale_x_continuous(breaks = 1:10, minor_breaks = NULL) +
    theme_minimal()) %>%
  ggplotly()

I know that overfitting is bad, but I need both lines to go straight through points (like in Excel).

Solution might be pure plotly (gives more control than ggplotly() transformation).

Additional, not required: Display Labels for points only (not smoothed curve)

Matiasko
  • 187
  • 9
  • Would the approach here (https://stackoverflow.com/questions/35205795/plotting-smooth-line-through-all-data-points) work in plotly for you? – Mark Neal Apr 25 '20 at 21:21

1 Answers1

15

You can add the function geom_xspline, mentioned in the site: https://www.r-bloggers.com/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines/

After that use the code:

p <- ggplot(df, aes(x, y, group=Group, color=factor(Group))) +
  geom_point(color="black") +
  geom_xspline(size=0.5)+
  geom_point(aes(color = Group)) +
  scale_y_continuous(limits = c(0, 0.35), labels = scales::percent) +
  scale_x_continuous(breaks = 1:10, minor_breaks = NULL) +
  theme_minimal()+
  geom_text(aes(label=y),hjust=1, vjust=-1)
p

Output will be: Plot Image

Hope this helps!

abhisekG
  • 426
  • 2
  • 5
  • 16
  • 5
    I found implementation of `geom_xspline()` in `ggalt` package. It truly does what I need as https://answers.microsoft.com/en-us/office/forum/office_2007-excel/how-does-excel-plot-smooth-curves/c751e8ff-9f99-4ac7-a74a-fba41ac80300 states that Excel used Catmull-Rom spline smoothing. Thus using `geom_xspline()` with `spline_shape = -0.5` does the job. Unfortunately this geom has yet to be implemented in `plotly`. – Matiasko Oct 13 '19 at 13:14
  • why is `geom_xspline()` called twice for the above ggplot? shouldn't the two curves be handled by `aes(color=factor(Group))` for one call? – Agile Bean Aug 23 '21 at 16:23
  • I have removed one of the geom_xspline() call. Thanks for pointing it out @AgileBean – abhisekG Aug 29 '21 at 02:53