Introduction
Interactive visualizations empower you to explore and communicate your data dynamically. With Quarto Live, you can build rich plots using ggplot2 and convert them into interactive visualizations with Plotly—all directly within your web browser. This tutorial will guide you through creating these visualizations, customizing their appearance, and troubleshooting common issues.
1. Building Interactive Visualizations with ggplot2
ggplot2 is a versatile plotting system for R. Start by creating a basic scatter plot using the mtcars
dataset.
Source
```{webr}
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Motor Trend Car Road Tests 🚀",
x = "Weight", y = "Miles per Gallon",
color = "Cylinders")
```
This example plots car weight against miles per gallon, with point colors representing the number of cylinders.
2. Converting ggplot2 Charts to Interactive Plotly Visualizations
Enhance your static ggplot2 chart by converting it into an interactive Plotly visualization. This allows you to hover over points, zoom, and pan for a richer data exploration experience.
Source
```{webr}
library(ggplot2)
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Interactive Plotly Visualization",
x = "Weight", y = "Miles per Gallon",
color = "Cylinders")
ggplotly(p)
```
The ggplotly()
function transforms your ggplot2 chart into an interactive Plotly object, enabling features like tooltips and zooming.
3. Customising Plot Dimensions
Adjust the visual appearance of your plots by setting custom dimensions using the fig-width
and fig-height
options. This helps tailor your visualizations to your document layout.
Source
```{webr}
#| fig-width: 8
#| fig-height: 4
plot(rnorm(10000), type = 'l')
```
These options ensure that your plot appears with the specified width and height, making it easier to integrate into your layout.
4. Interactive Exercise
Now it’s your turn! Modify the ggplot2 example to change the point size and apply a different color palette (e.g., “Set2”, “Dark2”) to see how the visualization changes. Experiment with the code below:
Exercise
```{webr}
# Modify the ggplot2 visualization:
library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) + # Try changing this value (e.g., size = 5)
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Customized Motor Trend Plot",
x = "Weight", y = "Miles per Gallon",
color = "Cylinders")
# Uncomment the next line and add a new scale, for example:
# p <- p + scale_color_brewer(palette = "Set2")
print(p)
```
5. Troubleshooting and Best Practices
Troubleshooting Tips
- Missing Packages:
If you receive an error indicating a missing package (e.g.,plotly
), refer to our Loading and Using Packages tutorial. - Browser Compatibility:
Ensure you are using a modern browser that supports the OffscreenCanvas API for optimal interactive performance. - Data Issues:
Verify that your dataset is correctly loaded and that there are no NA values affecting the plot.
Best Practices
- Keep It Simple:
Start with simple visualizations and gradually add layers for complexity. - Accessibility:
Use clear titles, axis labels, and color contrasts to ensure that your plots are accessible to all users. - Responsive Design:
Test your visualizations on different devices and adjust dimensions as needed.
Performance Considerations
- Data Volume:
For large datasets, consider sampling or filtering to maintain performance. - Efficient Reactivity:
Optimize reactive expressions to avoid unnecessary re-computation.
Further Reading
- Interactive Code Blocks Explained
Learn how to create and customize interactive code blocks in Quarto Live. - Loading and Using Packages
Discover techniques for installing and loading additional R packages. - Managing Execution Environments
Understand how to control variable sharing and isolate interactive sessions. - Cell Options Reference
Dive deeper into advanced configuration options for interactive code blocks.
Conclusion
This tutorial has demonstrated how to build interactive visualizations in R using ggplot2 and convert them into interactive Plotly charts with Quarto Live. You also learned how to customize plot dimensions, engaged in an exercise to modify a visualization, and reviewed troubleshooting tips and best practices. Experiment with these examples to create dynamic, engaging visualizations that enhance your data storytelling.
Explore More Articles
Here are more articles from the same category to help you dive deeper into the topic.
Reuse
Citation
@online{kassambara2025,
author = {Kassambara, Alboukadel},
title = {Interactive {Data} {Visualization} in {R}},
date = {2025-03-08},
url = {https://www.datanovia.com/learn/interactive/r/data-visualization.html},
langid = {en}
}