Open In App

Spline Chart using R

Improve
Improve
Like Article
Like
Save
Share
Report

Spline Chart can be defined as a type of line chart which is used to plot time-dependent variables. The basic difference between a Line and a spline chart is the points on the spline chart are connected with the help of curves instead of straight lines which ensures that the visualization is more informative to the analyst. In R Programming the spline charts can be implemented using Plotly Package.

Steps to plot Spline Chart in R

1. Install and load the required package

R




# Install plotly package
install.packages("plotly")
 
# Load the installed package
library(plotly)


2. Create or load dataframe where the time-dependent data present in it needs to be plotted. In this article, we are going to create a sample data frame that consists of admissions related to some colleges as follows:

R




# Creation of sample data frame
df<-data.frame(admissions=c(2016,2017,2018,2019,2020,2021,2022,2023),
               admissions_count=c(110,125,108,140,132,143,102,123))


3. Creation of trace so that it can be added to the plot using plotly of R.

R




trace <- list(
  line = list(shape = "spline"),
  mode = "lines+markers",
  name = "'spline'",
  type = "scatter",
  x = df$admissions,
  y = df$admissions_count
)


4. Plotting the Spline Chart using plot_ly function of plotly package

Syntax: plot_ly(df,type,marker,labels,values) %>% layout() %>% add_trace()
Where,

  •  df – data frame
  •  type – used to specify the type of plot we want to visualize
  •  marker – used to mark the plot with different colors using color attribute
  • labels – names of categorical variables in the dataset
  • values – values of the columns in the dataset that we want to plot are specified here (optional)
  • layout() –  this function is used to change the layout as required (like assigning a title to the plot)
  • add_trace() – this function is used to append similar new traces to existing dimension

R




# Plotting the plot using plotly
plot <- plotly::plot_ly()
 
# Adding trace to the plot
plot <- add_trace(plot, line=trace$line,
               mode=trace$mode,type=trace$type,
               x=trace$x, y=trace$y)
 
# Displaying the plot
plot


Output:

Spline Chart using Plotly

Line and Spline Chart on the same plot

Let’s plot the line and spline chart on the same plot to observe how they differ from each other.

R




# Load the plotly package
library(plotly)
 
# Creation of sample dataframe
df<-data.frame(admissions=c(2016,2017,2018,2019,2020,
                            2021,2022,2023),
               admissions_count=c(110,125,108,140,132,
                                  143,102,123))
 
# Adding line plot trace to the plot
trace1 <- list(
  line = list(shape = "Linear"),
  mode = "lines+markers",
  name = "'Line'",
  type = "scatter",
  x = df$admissions,
  y = df$admissions_count
)
# Adding spline plot trace to the plot
trace2 <- list(
  line = list(shape = "spline"),
  mode = "lines+markers",
  name = "'Spline'",
  type = "scatter",
  x = df$admissions,
  y = df$admissions_count
)
 
# Plotting the Combined chart
plot <- plot_ly()
plot <- add_trace(plot, line=trace1$line,
                  mode=trace1$mode, name=trace1$name,
                  type=trace1$type, x=trace1$x, y=trace1$y)
plot <- add_trace(plot, line=trace2$line, mode=trace2$mode,
                  name=trace2$name, type=trace2$type,
                  x=trace2$x, y=trace2$y)
plot


Output:

Line and Spline Chart using Plotly

Customized Spline Chart

The Spline chart can also be customized using layout() function which helps us to add a title to the plot, changes the background color of the plot, and also ensures us to add labels to the x and y-axis respectively as follows:

R




# Loading the plotly Package
library(plotly)
df<-data.frame(admissions=c(2016,2017,2018,2019,
                            2020,2021,2022,2023),
               admissions_count=c(110,125,108,140,
                                  132,143,102,123))
 
# Spline trace which need to be added to the plot
trace <- list(
  line = list(shape = "spline",color="red"),
  mode = "lines+markers",
  name = "'Spline'",
  type = "scatter",
  x = df$admissions,
  y = df$admissions_count
)
 
# Initializing the plot
plot <- plot_ly()
plot <- add_trace(plot, line=trace$line, mode=trace$mode,
                  name=trace$name, type=trace$type,
                  x=trace$x, y=trace$y)
 
# Customizing the layout of the plot
plot <- layout(plot,title="Customized Spline Chart",
               xaxis=list(title="Admissions Count"),
               yaxis=list(title="Academic Year"),
               plot_bgcolor='skyblue' )
plot


Output:

Customized Spline Chart using Plotly

Spline Regression in R

Spline regression is a special type of regression technique that is used to fit data in a less efficient way than linear and logistic regression models based on dividing the points as knots and connecting them with the nearest fitting curve. This type of regression is mostly used when the dataset we want to perform regression on is going to change in frequent intervals. The spline regression model in R can be implemented using methods present in the splines package.

Syntax: lm(formula, dataframe)
Where,
 formula – the formula for the linear model is specified here
 dataframe – data frame is specified here

R




# Install and load splines package
install.packages("splines")
library(splines)
 
# Creation of sample dataframe
df<-data.frame(admissions=c(2016,2017,2018,2019,2020,2021,2022,2023),
               admissions_count=c(110,125,108,140,132,143,102,123))
 
# Fitting spline the regression model
spline_fit <- lm(df$admissions_count ~ bs(df$admissions))
 
# Calculate predictions using spline regression model
x_limits <- range(df$admissions)
x_grid <- seq(x_limits[1], x_limits[2])
preds <- predict(spline_fit, newdata=list(x=x_grid))
 
# Spline plot with calculated spline regression predictions
plot(df$admissions, df$admissions_count,xlab="Admission Year",
     ylab="Admission_Count")
lines(x_grid, preds)


Output:

Spline Regression using R



Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads