smoothing of line

Author

P K Parida

Smoothing of line

This is a code to prepare a smoothing line

Line diagram

Preparation of normal line diagram

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#devtools::install_github("hrbrmstr/ggalt")
library(ggalt)
Registered S3 methods overwritten by 'ggalt':
  method                  from   
  grid.draw.absoluteGrob  ggplot2
  grobHeight.absoluteGrob ggplot2
  grobWidth.absoluteGrob  ggplot2
  grobX.absoluteGrob      ggplot2
  grobY.absoluteGrob      ggplot2
set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10),
                  y=c(sample(15:30, 10), 2*sample(15:30, 10), 3*sample(15:30, 10)),
                  group=factor(c(rep(1, 10), rep(2, 10), rep(3, 10))) )

ggplot(dat, aes(x, y, group=group, color=factor(group))) +
  geom_point(color="steelblue") +
  geom_line()

Now the lines can be smoothend by using ggalt pacakage function geom_xspline (spline_shape= -0.2 to -0.4)

ggplot(dat, aes(x, y, group=group, color=factor(group))) +
  geom_point(color="steelblue") +
  geom_xspline(size=0.5, spline_shape=-0.3) 
Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.

The echo: false option disables the printing of code (only output is displayed).