Open In App

How to create interactive data visualizations with ggvis

Last Updated : 24 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  1. layer_points(): It helps in adding points to the plot.
  2. layer_bars(): It helps in adding add bars to the plot.
  3. layer_lines(): It helps in adding lines to the plot.
  4. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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:

GGVIS-Visualization-1

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:

GGVIS-Visualization-2

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:

GGVIS-Visualization-3

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:

GGVIS-Visualization-4

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 %&gt;%
 ggvis(x = ~Sepal.Length, y = ~Sepal.Width, stroke = ~factor(Petal.Length)) %&gt;%
 layer_points()

Output:

GGVIS-Visualization-Stroke

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:

GGVIS-Visualization-StrokeDash

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:

GGVIS-Visualization-LayersHistogram1

Now we plot histogram With customize Width

GGVIS-Visualization-LayersHistogram2

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:

GGVIS-Visualization-LayerBars1

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:

GGVIS-Visualization-LayersText

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:

GGVIS-Visualization-LayersRibbon

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:

GGVIS-Visualization-LayersBosplot

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:

GGVIS-Visualization-MultiLayer2

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:

RB1


RB2

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:

NI1


NI2

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:

D1

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.

D2

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.




Next Article

Similar Reads