VOOZH about

URL: https://www.geeksforgeeks.org/r-language/creating-interactive-plots-using-shiny/

⇱ Creating Interactive Plots using Shiny - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Creating Interactive Plots using Shiny

Last Updated : 5 Jun, 2026

Interactive data visualization allows users to explore data dynamically instead of viewing static charts. By using tools like Shiny in R, we can build web applications that respond to user inputs such as dropdowns, sliders and buttons. This makes data analysis more engaging, flexible and insightful.

  • Shiny enables creation of interactive web apps directly from R code.
  • Users can modify inputs and see real-time changes in plots and outputs.
  • It integrates easily with visualization libraries like ggplot2 and plotly.

Shiny App

Shiny is an R package that simplifies the process of building web applications directly from R code. It provides a framework for creating interactive web applications without requiring expertise in web development technologies such as HTML, CSS or JavaScript.

It includes two key components:

  1. User Interface (UI): The UI component shows a blueprint for defining the visual elements of the Shiny app using R functions that generate HTML.
  2. Server: It reacts responsively to user input, processes data and generates dynamic output predicated on user interactions.

Syntax

library(shiny)
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui = ui, server = server)

K-Means Clustering using Shiny

In this example, users can select X and Y variables from the iris dataset and choose the number of clusters.

Step 1: Install and load required packages

Step 2: Define the UI

Create a UI with dropdown lists (select inputs) for choosing the X and Y variables and a slider for selecting the number of clusters.

Step 3: Define the server logic

Step 4: Run the Shiny app

Combine the UI and server functions using `shinyApp()` and run the app.

Output:

👁 ezgif-1-e402573ba4
Plotting K-Means Clustering Using Shiny

Create a UI layout with dropdown lists (select inputs) and a slider for selecting variables and clusters.

  • Create a UI using fluidPage() with a titlePanel() and sidebarLayout().
  • Add selectInput() for X and Y variables and sliderInput() for number of clusters inside sidebarPanel().
  • Display the plot in mainPanel().
  • In the server, use renderPlot() to apply kmeans() based on user inputs.
  • Plot clusters using plot() and points().
  • Combine UI and server using shinyApp() and run the app.

Creating Interactive Plots using Shiny

An interactive plot allows users to explore data dynamically instead of viewing a fixed graph. Unlike static plots, interactive visualizations respond to user inputs such as dropdown selections, sliders and buttons. Using Shiny and Plotly, we can create responsive plots that update in real time.

Interactive Scatter Plot (Shiny + Plotly)

Output:

👁 ezgif-1-8b8a267062
Interactive Plotting using Plotly

Explanation:

  • Users select X and Y variables dynamically.
  • The scatter plot updates automatically.
  • Hover, zoom and pan features are enabled.

Creating Customized Interactive Plot

This version allows more user control such as selecting plot type and adding regression lines.

Output:

👁 ezgif-6-2a6fcd6ec3
Customized Interactive Plot

Explanation: Users can select the plot type (Scatter Plot, Box Plot, Bar Plot) via radio buttons and select variables for X and Y axes with dropdown menus.

  • They can add a line, smooth line (with adjustable span) and regression line using checkboxes.
  • checkboxInput() creates a checkbox that lets users enable or disable an option.
  • Users can set limits on the X-axis using a slider.
  • Customize the plot title using a text input.
  • Download option is available for the plot as an HTML file.

Creating Heatmap using Shiny

Output:

👁 ezgif-6-860692ba69
Creating Interactive Heatmap

Explanation: Users can select the variables for the X-axis (x_var), Y-axis (y_var) and Z-axis (z_var) from dropdown menus.

  • They can choose the color scale for the heatmap from a predefined list (color_scale).
  • Users can customize the plot title using a text input (plot_title).
  • The "Refresh Plot" button allows users to update the plot with the selected options.
  • There's an option to download the plot as an HTML file.
Comment
Article Tags:
Article Tags:

Explore