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.packages ( "plotly" )
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
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
plot <- plotly:: plot_ly ()
plot <- add_trace (plot, line=trace$line,
mode=trace$mode,type=trace$type,
x=trace$x, y=trace$y)
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
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))
trace1 <- list (
line = list (shape = "Linear" ),
mode = "lines+markers" ,
name = "'Line'" ,
type = "scatter" ,
x = df$admissions,
y = df$admissions_count
)
trace2 <- list (
line = list (shape = "spline" ),
mode = "lines+markers" ,
name = "'Spline'" ,
type = "scatter" ,
x = df$admissions,
y = df$admissions_count
)
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
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))
trace <- list (
line = list (shape = "spline" ,color= "red" ),
mode = "lines+markers" ,
name = "'Spline'" ,
type = "scatter" ,
x = df$admissions,
y = df$admissions_count
)
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)
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.packages ( "splines" )
library (splines)
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_fit <- lm (df$admissions_count ~ bs (df$admissions))
x_limits <- range (df$admissions)
x_grid <- seq (x_limits[1], x_limits[2])
preds <- predict (spline_fit, newdata= list (x=x_grid))
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
Share your thoughts in the comments
Please Login to comment...