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:
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: