How to create interactive data visualizations with ggvis
Last Updated :
24 May, 2024
Creating interactive data visualizations is a powerful way to explore and present data. The ggvis
package in R provides a flexible framework for building these visualizations by combining the capabilities of dplyr
data manipulation and Shiny
interactivity. This article will guide you through the process of creating interactive data visualizations using ggvis
.
Introduction to ggvis
ggvis
is an R package designed for creating interactive graphics. It allows you to create a wide variety of visualizations and supports interactivity, making it ideal for exploratory data analysis and dynamic reporting.
Layer Functions and Plotting Parameters in ggvis
The ggvis
package in R offers a variety of layer functions and plotting parameters to create rich, interactive visualizations. Below is a combined overview of the layer functions available ggvis
and the parameters you can use to customize your plots.
- layer_points(): It helps in adding points to the plot.
- layer_bars(): It helps in adding add bars to the plot.
- layer_lines(): It helps in adding lines to the plot.
- layer_paths(): It helps in adding paths (connected lines) to the plot.
Plotting parameters allow you to customize the appearance and behavior of the layers in your ggvis
plots. Here are the primary plotting parameters:
- fill: This parameter determines the fill color of the points. You can specify a color or use a variable to map different colors to the points based on categories or values.
- fillOpacity: This parameter controls the transparency of the fill color of the points. It takes a value between 0 and 1, where 0 represents transparent and 1 represent being opaque.
- stroke: This parameter sets the color of the outline or border of the points. It can be specified as a color name or a variable for mapping different colors.
- size: This parameter is used to control the size of the points. You can specify a constant size or use a variable to map different sizes to the points based on values.
Installing and Loading ggvis
Before you can start creating visualizations, you need to install and load the ggvis
package:
R
# Installation:
install.packages("ggvis")
# Loading the package into R:
library(ggvis)
1. Displaying Points using ggvis
The ggvis
package in R, you can easily create visually appealing scatter plots by displaying points.
R
Plot1 <- ggvis(iris, x = ~Sepal.Length, y = ~Sepal.Width)
layer_points(Plot1)
# Alternatively We can also use
layer_points(ggvis(iris, x = ~Sepal.Length, y = ~Sepal.Width))
Output:

Data Visualization With ggvis
Displaying Points with Colors, Shapes, Size, and Stroke
We can add more variables to the plot by mapping them to other visual properties like fill
, size,
 shape
And stroke.
R
iris %>%
ggvis( x = ~Sepal.Length, y = ~Sepal.Width, fill = ~Petal.Width) %>%
layer_points()
Output:

Changing Size using ggvis
The ggvis
package in R, you can easily change the size of points to enhance the visual representation of your data.
R
iris %>%
ggvis( x = ~Sepal.Length, y = ~Sepal.Width, size = ~Petal.Width) %>%
layer_points()
Output:

Changing Shape using ggvis
The ggvis
package in R, you can easily change the shape of points to enhance the visual representation of your data.
R
iris %>%
ggvis(x = ~Sepal.Length, y = ~Sepal.Width, shape = ~factor(round(Petal.Width,0)))%>%
layer_points()
Output:

Changing Stroke using ggvis
The ggvis
package in R, you can easily change the stroke of points to enhance the visual representation of your data.
R
iris %>%
ggvis(x = ~Sepal.Length, y = ~Sepal.Width, stroke = ~factor(Petal.Length)) %>%
layer_points()
Output:

2. Displaying Dotted Lines (using strokeDash)
With the ggvis
package in R, you can easily display dotted lines by adjusting the stroke dash pattern.
R
W <- seq(0, 10, 0.1)
data <- data.frame(fit=3+2*W, upper=4+2*W, lower=2+2*W)
strokes <- data %>%
ggvis(x= ~fit, y= ~W) %>%
layer_lines() %>%
layer_lines(x= ~lower, y= ~W, strokeDash:=6) %>%
layer_lines(x= ~upper, y= ~W, strokeDash:=6)
strokes
Output:

3. Displaying Histograms
With the ggvis
package in R, you can create interactive histograms to explore your data effectively.
R
iris %>%
ggvis(~Sepal.Length) %>%
layer_histograms()
# With Width
iris %>%
ggvis(~Sepal.Length) %>%
layer_histograms(width = 0.5, center = 0)
Output:

Now we plot histogram With customize Width

ggvis Width Histogram
4. Displaying Stacked Bar Graphs
Stacked bar graphs are effective visualizations for displaying the distribution of categorical variables and comparing proportions within each category.
R
# Stacked bars
# If grouping var is continuous, you need to manually specify grouping
ToothGrowth %>%
group_by(dose) %>%
ggvis(x = ~supp, y = ~len, fill = ~dose) %>%
layer_bars()
Output:

5. Displaying Texts using ggvis
With the ggvis
package in R, you can easily display text elements to annotate your visualizations and highlight important points.
R
df <- data.frame(x = 3:1, y = c(1, 3, 2), label = c("a", "b", "c"))
df %>%
ggvis(~x, ~y, text := ~label) %>%
layer_text(fontSize := 50)
Output:

6. Displaying Ribbon using ggvis
With the ggvis
package in R, you can easily display Ribbon your visualizations and highlight important points.
R
df <- data.frame(x = seq(0,10,by=0.1), y = seq(0,10,by=0.1))
df %>%
ggvis( x = ~x, y = ~y + 0.3, y2 = ~y-0.3 ) %>%
layer_ribbons()
Output:

7. Displaying Box Plots using ggvis
With the ggvis
package in R, you can easily display Box Plots your visualizations and highlight important points.
R
iris %>%
ggvis( x = ~factor(Species), y = ~Sepal.Length) %>%
layer_boxplots()
Output:

We used ggvis to initialize the plot and mapped the Species column to the x-axis using ~factor(Species) and the Sepal.Length
column to the y-axis using ~Sepal.Length. Then, it adds box plots to the plot to visualize the distribution of Sepal. Length for each species of iris flowers.
8. Displaying Regression Lines
With the ggvis
package in R, you can easily display Regression Lines your visualizations and highlight important points.
R
iris %>%
ggvis(x = ~Sepal.Length, y = ~Petal.Length) %>%
layer_points() %>%
layer_model_predictions(model = "lm", stroke := "green", se=T)
Output:

9. Using the Radio Button
Change the Model Type & Line Color using the Radio Button.
R
mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points() %>%
layer_model_predictions(
model = input_radiobuttons(c("LOESS" = "loess", "Linear" = "lm"),
label = "Model type"),
stroke := input_radiobuttons(c("Red" = "red", "Black" = "black"),
label = "Line color")
)
Output:


10. Taking Numeric Input
Change the size of the Point and number of Interpolation Points through numeric Input.
R
mtcars %>%
ggvis(x = ~wt, y = ~mpg) %>%
layer_points(size := input_numeric(value = 25, label = "Point size")) %>%
layer_smooths(span = input_numeric(value = 0.5, label = "Interpolation points"))
Output:


11. Using the Mapping Function
Creating a function mapping different values to two sets and using the Input Select to Display the Required set.
R
new_vals <- input_select(c("Set A" = "A", "Set B" = "B"),
label = "Dynamically-generated column",
map = function(value) {
vals <- switch(value,
"A" = c("One", "Two"),
"B" = c("First", "Second", "Third", "Fourth"))
rep(vals, length = nrow(mtcars))
})
mtcars %>%
ggvis(x = ~wt, y = ~mpg, fill = new_vals) %>%
layer_points()
Output:

In this example, I have added two values for the user to select input as Set A or Set B. In this, I have used the map function to map the values of Set A to “One” and “Two” and Set B to “First”, “Second”, “Third”, and “Fourth”. The switch statement takes the selected value from the dropdown menu(value) and, On the basis of its value, assigns different sets of values to the variable.

Conclusion
In conclusion, ggvis offers a flexible and intuitive framework for creating interactive data visualizations in R. By following the steps outlined in this guide and experimenting with the various features and customization options available in ggvis, users can effectively communicate data insights and facilitate data exploration in a dynamic and engaging manner.
Similar Reads
Interactive Data Visualization with Python and Bokeh
In this article, we'll learn how to do Interactive Data Visualization with Bokeh. Bokeh is a Python library that is used for creating interactive visualizations for modern web browsers. It handles custom or specialized use cases very simply. It provides high-performance interactive charts and plots.
8 min read
What is Interactive Data Visualization?
Organizations are always looking for innovative methods to effectively share insights and get value from their data in today's data-rich environment. With dynamic and engaging images, users may explore and comprehend data thanks to the potent interactive data visualization technology. The article ai
9 min read
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
9 min read
Interactive Data Visualizations in R Using ggiraph
Interactive data visualizations can significantly enhance the ability to explore and understand complex datasets. In R, the ggiraph package allows you to create interactive versions of ggplot2 visualizations. This article will provide an overview of ggiraph, its key features, and step-by-step exampl
5 min read
10 Examples of Interactive Map Data Visualizations
In today's data-driven world, interactive map data visualizations have become a powerful tool for communicating complex information visually and intuitively. By the term 'map' we can say it's related to geographic area. Thus the topic describes the visualization or analysis of geographic insight and
7 min read
How to Save Time with Data Visualization using Stack in R with ggplot2
The widely used R package ggplot2 is used to produce beautiful and efficient data visualisations. Here are some pointers for speeding up data visualisation using the "stack" feature of ggplot2: Select the pertinent information: Make sure the data you plan to use in your visualisation is appropriate.
6 min read
Interactive visualization of data using Bokeh
Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If youâve worked with visualization in Python before, itâs likely that you have used matplotlib. But Bokeh differs from matp
4 min read
Visualization a Linear Model on a Scatterplot with ggvis
It is a statistical model used to describe the relationship between a dependent variable and one or more independent variables. The linear model is used in data analysis. We can say that a linear model assumes a linear relationship between the dependent variable and each independent variable. Linear
3 min read
Master Data Visualization With ggplot2
In this article, we are going to see the master data visualization with ggplot2 in R Programming Language. Generally, data visualization is the pictorial representation of a dataset in a visual format like charts, plots, etc. These are the important graphs in data visualization with ggplot2, Bar Ch
8 min read
Getting started with Data Visualization in R
Data visualization is the technique used to deliver insights in data using visual cues such as graphs, charts, maps, and many others. This is useful as it helps in intuitive and easy understanding of the large quantities of data and thereby make better decisions regarding it. Data Visualization in R
6 min read