
Image by Editor | Midjourney
Interactive dashboards are a great way to explore and share data. They let users interact with the data and see insights in real time. In R, you can create these dashboards by using Shiny and ggplot2 together. Shiny helps make the dashboard interactive, while ggplot2 creates the charts and graphs.
In this article, we will show you how to combine Shiny and ggplot2 to build a dynamic dashboard.
Setting Up Your Environment
First, ensure that you have the required packages installed. You can install them using the following commands:
install.packages("shiny")
install.packages("ggplot2")
install.packages("dplyr")
Load these libraries at the beginning of your R script:
library(shiny) library(ggplot2) library(dplyr)
Understanding Shiny’s Architecture
A Shiny app consists of two main components:
- UI (User Interface): Defines the layout and appearance of the app
- Server: Contains the logic that runs behind the scenes to handle inputs and generate outputs
The basic structure of a Shiny app looks like this:
ui <- fluidPage(
# UI elements go here
)
server <- function(input, output) {
# Server logic goes here
}
shinyApp(ui = ui, server = server)
Defining the UI
In Shiny, the UI is created using the fluidPage() function. It provides a flexible layout. We will add an input widget that lets users choose a variable to plot. Based on the selection, the app will show the corresponding plot.
ui <- fluidPage(
titlePanel("Interactive Dashboard with Shiny and ggplot2"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Choose a variable to plot:",
choices = c("mpg", "hp", "wt"),
selected = "mpg")
),
mainPanel(
plotOutput("plot")
)
)
)
Creating the Server Logic
The server function defines the behavior of the app. In this case, we will generate a plot using ggplot2 based on the user’s input. We will use the renderPlot function to create the plot and ggplot() to define the plotting logic.
server <- function(input, output) {
output$plot <- renderPlot({
# Filter the dataset based on user input
variable <- input$variable
# Create the ggplot based on the chosen variable
ggplot(mtcars, aes_string(x = "mpg", y = variable)) +
geom_point() +
labs(x = "Miles per Gallon (mpg)", y = variable, title = paste("mpg vs", variable)) +
theme_minimal()
})
}
Running the Shiny App
To launch the interactive dashboard, combine the UI and server components into a shinyApp() function call.
shinyApp(ui = ui, server = server)
When you run the code, the Shiny app will open in your web browser. Users can select a variable from the dropdown. The plot will update automatically based on the chosen variable.
Click here to view the output.
Adding More Interactivity
To make your Shiny app more engaging, you can add additional features such as sliders and checkboxes. These elements enhance the user’s ability to filter, customize, and explore the data further.
- sliderInput(): Creates a slider to select a range of values
- checkboxInput(): Creates a checkbox to enable or disable gridlines
# Define UI
ui <- fluidPage(
titlePanel("Interactive Dashboard with Shiny and ggplot2"),
sidebarLayout(
sidebarPanel(
# Dropdown for variable selection
selectInput(
"variable",
"Choose a variable to plot:",
choices = c("mpg", "hp", "wt"),
selected = "mpg"
),
# Slider input for MPG range
sliderInput(
"mpgRange",
"Select MPG Range:",
min = min(mtcars$mpg),
max = max(mtcars$mpg),
value = c(min(mtcars$mpg), max(mtcars$mpg))
),
# Checkbox to toggle gridlines
checkboxInput(
"showGrid",
"Show Gridlines",
value = TRUE
)
),
mainPanel(
# Output for the plot
plotOutput("plot")
)
)
)
# Define Server
server <- function(input, output) {
# Render the plot
output$plot <- renderPlot({
variable <- input$variable
# Build the ggplot
p <- ggplot(mtcars, aes_string(x = "mpg", y = variable)) +
geom_point(color = "blue", size = 3) +
labs(
x = "Miles per Gallon (mpg)",
y = variable,
title = paste("MPG vs", variable)
) +
theme_minimal()
# Conditionally modify gridlines based on the checkbox
if (input$showGrid) {
p <- p + theme(
panel.grid.major = element_line(),
panel.grid.minor = element_line()
)
} else {
p <- p + theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
}
p # Return the plot
})
}
# Run the app
shinyApp(ui = ui, server = server)
Click here to view the output.
Conclusion
Creating interactive dashboards in R with Shiny and ggplot2 is a great way to explore and present data. By combining Shiny’s interactive features with ggplot2’s visualizations, you can build dynamic apps. You can add several elements like sliders and checkboxes to make the dashboard more engaging and customizable.
