Open In App

Interactive Data Visualization with Plotly Express in R

Data Visualization in R is the process of representing data so that it is easy to understand and interpret. Various packages are present in the R Programming Language for data visualization.

Plotly’s R graphing library makes interactive, publication-quality graphs. Plotly can be used to make various interactive graphs such as scatter, line, bar, histogram, heatmaps, and many more. It is based on the Plotly.js JavaScript library which is used for making interactive graphical visualization.



Plotly supports a wide range of features including animation, legends, and tooltips.

Installation

To make interactive data visualization you first need to install R and R studio on your machine and then can install Plotly by running the below command in R studio



install.packages("plotly")                                              

Now we can use the Plotly package in r using the below code

library(plotly)

Creating a basic scatter plot using the iris data set and plot_ly function:




install.packages("plotly")
library(plotly)
 
variable <- plot_ly(iris, x=~Petal.Length, y=~Petal.Width)
variable

Output:

Interactive Data Visualization with Plotly Express in R

Scatter Plot

In scatter plot shows variation of one variable with respect to another variable. We plot one variable on the x-axis and another on y-axis. The relationship between the two variables is showed with a dot. We can change the dot size, color to show the relationship between the variables.

For plotting scatter plot we are going to use the mtcars dataset. We are going to map the mpg property on the xaxis and disp property on the yaxis.




library(plotly)
graph <- mtcars %>%
  plot_ly(x=~mpg, y=~disp, type="scatter", color=~cyl) %>%
  layout(
    title="Miles per gallon vs Displacement",
    xaxis=list(
      title = "Miles per gallon",
      range = c(0,50)
    ),
    yaxis=list(
      title = "Displacement",
      range=c(0,500)
    )
  )
graph

Output:

The points are colored based on the cyl attribute present in mtcars dataset.




install.packages("gapminder")
library(gapminder)
animatedscatter <- gapminder %>%
  plot_ly(x=~log(gdpPercap), y=~lifeExp, frame=~year, color=~year, type="scatter") %>%
  layout(
    title=list(
      text="Fuel Efficiency",
      font = list(color = "black"),
      title_pad = 100),margin = list(t = 50),
    paper_bgcolor='rgb(128,128,128)',
    plot_bgcolor = 'rgb(128,128,128)',
    xaxis=list(
        title = "log(GdpPerCapita)",
        color="black",
        linecolor="black"
    ),
    yaxis=list(
        title = "LifeExp",
        color="black",
        linecolor="black"
    )
  )
animatedscatter

Output:

in the above code we used the gapminder dataset to draw animated scatter plot. The above code showed the life expentency and gdp per capita for all the years.

Line Plot

Line plot is similar to scatter plot but in this we add connect the two dots together to form a line. We can draw multiple lines with different color to show relation between the x and different y axis.

For drawing the line plot we are going to use the economics dataset. We are to plot the date on the x-axis and then see how unemploy rate changes with the date using line plot.




library(plotly)
graph <- economics %>%
  plot_ly(x=~date) %>%
  add_trace(y=~unemploy/400, type="scatter", mode="lines")
 
graph

Output:

Multiline Plot:

We can also add multiple lines to the same plot using the add_trace function. The line plot created will be of different color for each different y attribute we specify in the add_trace function.




library(plotly)
plot <- economics %>%
  plot_ly(x=~date)  %>%
  add_trace(y=~unemploy/400, type="scatter", mode="lines", name="unemployed") %>%
  add_trace(y=~uempmed, type="scatter", mode="lines", name="unemployment rate") %>%
  layout(
    title=list(
      text="date vs (unemployed and unemployment rate)",
      font = list(color = "white"),
      title_pad = 100
    ),
    margin = list(
      t = 50
    ),
    paper_bgcolor='rgb(0,0,0)',
    plot_bgcolor = 'rgb(0,0,0)',
    legend = list(
      bgcolor = "white",
      font=list(
        family="sans-serif",
        color="red"
         
      )
    ),
    xaxis=list(
      title = "Date",
      rangeslider = list(type="date"),
      color = "white",
      linecolor="white",
      tickangle = -45,
      margin(
        t=10,
      )
    ),
    yaxis=list(
      title = "unemployed and unemployment rate",
      color = "white",
      linecolor="white",
      tickangle = -45,
      title_standoff=10
    )
  )
plot

Output:

Interactive Data Visualization with Plotly Express in R

Box Plot:

Box plot is used to see the distribution of data for a variety of classes. A box plot display 5 infomation about a class min, first quartile, median, second quartile and max. The box is drawn by connecting the first and second quartile of the data.




library(plotly)
boxplot <- mtcars %>%
  plot_ly(x=~factor(cyl), y=~mpg) %>%
  add_trace(type="scatter", name="scatter") %>%
  add_boxplot(name="Boxplot") %>%
  layout(
    title="Fuel Efficiency"
  )
boxplot

Output:

Interactive Data Visualization with Plotly Express in R

Now we can plot multiple boxplot using boxmode grouping function.




fig <- plot_ly(diamonds, x = ~cut, y = ~price, color = ~clarity, type = "box") %>%
  layout(boxmode = "group", title="CUT vs PRICE")
 
fig

Output:

Interactive Data Visualization with Plotly Express in R

We will now draw box plot using the diamonds dataset. Diamond dataset contains information such as price and other attributes for almost 54,000 diamonds. We will draw a box plot plot for each cut of the diamond vs its price.

3d Scatter Plot

In 3d plot we map x, y and z axis to three different attributes of the dataset. We are going to consider the iris dataset. We will map the Sepal.Length to x axis, Sepal.Width to y-axis and Petal.Length to the z axis. Even if we do not specify the type of plot to be scatter3d the plot_ly function automatically assumes it to be a scatter 3d plot.




plot_ly(data=iris,x=~Sepal.Length,
        y=~Petal.Length,
        z=~Sepal.Width,
        color=~Species)

Output:

Interactive Data Visualization with Plotly Express in R

Heatmap:

A heatmap is a two-dimensional graphical representation of data where the individual values that are contained in a matrix are represented as colors.




# Load necessary libraries
library(plotly)
 
# Load the iris dataset
data(iris)
 
# Calculate the correlation matrix
cor_matrix <- cor(iris[, 1:4])
 
# Create a heatmap using Plotly
heatmap <- plot_ly(
  x = colnames(cor_matrix),
  y = colnames(cor_matrix),
  z = cor_matrix,
  type = "heatmap",
  colorscale = "Viridis"
)
 
# Customize the layout
layout(heatmap, title = "Correlation Heatmap of Iris Dataset")
 
# Display the heatmap
print(heatmap)

Output:

Interactive Data Visualization with Plotly Express in R


Article Tags :