Open In App

How to embed ggvis interactive charts in RMarkdown

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The ggvis is an R package that provides a system for creating the interactive data visualizations. RMarkdown is a tool for creating dynamic documents that combine code, text, and output. 

To embed ggvis interactive charts in RMarkdown,  use the ggvisOutput and renderGgvis functions from the ggvis package.

RMarkdown

RMarkdown is a file format that allows for the integration of R code, output, and text into a single document.

  •  It is a powerful tool for creating reproducible reports, presentations, and  documents that require the use of R code.
  • RMarkdown documents can be rendered in  variety of formats, including HTML, PDF, Word, and more.

 Here are some of the key features of RMarkdown:

  1. Markdown formatting: RMarkdown uses Markdown syntax to format text, making it easy to create headings, lists, links, and other text elements in the document. 
  2. Code chunks: RMarkdown allows you to include code chunks in your document, which can be executed and the results included in the output. This allows you to embed code and output directly in your document, making it easier to reproduce and share the your work. Code.
  3. Multiple output formats: RMarkdown can generate a wide variety of output formats, including HTML, PDF, Word, and even presentations. This allows you to create the  document once and easily convert it to different formats depending on the your needs.
  4. Shiny integration: RMarkdown supports embedding Shiny apps in your documents  allowing you to create interactive documents that allow readers to interact with your code and data.
  5. Parameterized reports: RMarkdown allows you to create the  parameterized reports, where readers can specify inputs and generate customized outputs. 
  6. Customization: RMarkdown provides many options for customizing the appearance and behavior of your document, including custom CSS and JavaScript, LaTeX templates and more.

Steps :

1. Create the ggvis chart using your data.

R




library(ggvis)


2. Create a data frame and pipe it into ggvis() to create the interactive chart.

R




mtcars %>%
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  add_axis("x", title = "Weight") %>%
  add_axis("y", title = "Miles per Gallon")


3. Create a placeholder for the chart using ggvisOutput().

R




ggvisOutput("my_plot")


4. Use renderGgvis() to render the chart and pass in the ggvis()

R




renderGgvis({
  mtcars %>%
    ggvis(~wt, ~mpg) %>%
    layer_points() %>%
    add_axis("x", title = "Weight") %>%
    add_axis("y", title = "Miles per Gallon")
})


Example :

R




library(ggvis)
library(htmlwidgets)
 
# Define a ggvis plot
mtcars %>%
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  add_tooltip(function(df) paste0("Weight: ", df$wt, "<br>",
                                  "Miles per gallon: ", df$mpg))
 
widget <- last_plot() %>% htmlwidgets::onRender(
  "function(el, x) {
    el.ggvis = x;
    $(el).data('ggvis', el.ggvis);
  }"
)
widget


Output :

 

  • library(ggvis) and library(htmlwidgets) – These lines load the necessary packages for our example. ggvis is the package that provides the ggvis function for creating interactive charts, and htmlwidgets is a package for creating standalone HTML widgets from R objects.
  • mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% add_tooltip(function(df) paste0(“Weight: “, df$wt, “<br>”, “Miles per gallon: “, df$mpg)) – This code creates a ggvis plot of the mtcars dataset with weight on the x-axis and miles per gallon on the y-axis, using the ggvis() function. The layer_points() function adds a layer of points to the plot, and the add_tooltip() function 
  • last_plot() %>% – This code retrieves the most recently created ggvis plot.
  • htmlwidgets::onRender(“function(el, x) { el.ggvis = x; $(el).data(‘ggvis’, el.ggvis); }”) – This code creates a JavaScript function that sets the ggvis plot as a data attribute of  HTML element.
  • widget – This code combines the ggvis plot and the JavaScript function into a standalone HTML widget, which can be used in web pages or other HTML documents.

R




library(ggvis)
library(dplyr)
 
mtcars %>%
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_smooths(span = 0.2)


  • This code creates a ggvis plot of the mtcars dataset with weight the x-axis and miles per gallon on the y-axis, using the ggvis() function. 
  • The plot includes a layer of points and a smoothed line using layer_points() and layer_smooths() functions. 
  • The dplyr package is also loaded, which is used by the %>% pipe operator to the chain together the ggvis() and layer functions.

output :

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads