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 Chart using R

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

Similar Reads

Line and Spline Chart on the same plot

...

Customized Spline Chart

...

Spline Regression in R

...