0% found this document useful (0 votes)
3 views18 pages

Interactive Visualization in R

The document discusses interactive visualization in R, highlighting the importance of adding interactivity to data presentations for better engagement and exploration. It introduces several packages such as Plotly, Bokeh, and Leaflet for creating interactive plots and maps, along with examples of how to implement them. Additionally, it covers the Shiny package for building web applications that facilitate interactive data visualizations without extensive web development knowledge.

Uploaded by

naklimail100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Interactive Visualization in R

The document discusses interactive visualization in R, highlighting the importance of adding interactivity to data presentations for better engagement and exploration. It introduces several packages such as Plotly, Bokeh, and Leaflet for creating interactive plots and maps, along with examples of how to implement them. Additionally, it covers the Shiny package for building web applications that facilitate interactive data visualizations without extensive web development knowledge.

Uploaded by

naklimail100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Interactive

Visualization in R
Interactive Visualization

• Adding interactivity to a visualization provides an additional


mechanism through which data can be presented in an engaging,
efficient, and communicative way.
• Interactions can allow users to effectively explore large data sets by
panning and zooming through plots, or by hovering over specific plot
geometry to gain additional details on demand.
• The first two(Plotly and Bokeh) are able to add basic interactions to the
plots you might make with ggplot2, while the third (Leaflet) is used to
create interactive map visualizations.
• Picking among these (and other) packages depends on the type of
interactions you want your visualization to provide, the ease of use,
the clarity of the package documentation, and your aesthetic
preferences.
Plotting iris dataset using ggplot2

library(ggplot2)
data(iris)
ggplot(data = iris) +
geom_point(mapping =
aes(x = Sepal.Width, y =
Petal.Width, color =
Species))
The plotly package

• Plotly is a piece of visualization software that provides


open source APIs (programming libraries) for creating
interactive visualizations in a wide variety of languages,
including R, Python, Matlab, and JavaScript.
• By default, Plotly charts support a wide range of user
interactions, including tooltips on hover, panning, and
zooming in on selected regions.
• install.packages("plotly") # once per machine
• library("plotly") # in each relevant script
Interactive plots using plotly

• Two main ways to create interactive plots.


• First, you can take any plot created using ggplot2 and
“wrap” it in a Plotlyplot, thereby adding interactions to it.
• You do this by taking the plot returned by the ggplot()
function and passing it into the ggplotly() function provided
by the plotly package:
• # Create (and store) a scatterplot of the `iris` data set
using ggplot2 flower_plot <- ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Width, y =
Petal.Width, color = Species))
• # Make the plot interactive by passing it to Plotly's
`ggplotly()` function ggplotly(flower_plot)
plot_ly(
data =iris, #pass in the data to be visualized
x= ~Sepal.Width, #use a formula to specify the column
for the x-axis
y= ~Petal.Width, #use a formula to specify the column
for the y-axis
color= ~Species, #use a formula to specify the color
encoding
type ="scatter", #specify the type of plot to create
mode ="markers" #determine the “ drawing mode“ for
the scatter (points)
)
#Create a plot, then pipe that plot into the `layout()`function to modify it
#(Example adapted from the Plotly documentation)
plot_ly(
data= iris, #pass in the data to be visualized
x= ~Sepal.Width, #use a formula to specify the column for the x-axis
y= ~Petal.Width, #use a formula to specify the column for the y-axis
color = ~Species, #use a formula to specify the color encoding
type= "scatter", #specify the type of plot to create
mode= "markers" #determine the "drawing mode“ for the scatter (points)
)%>%
layout(
title= "Iris Data Set Visualization", #plot title
xaxis= list(title= "Sepal Width", ticksuffix= "cm"), #axis label + format
yaxis= list(title= "Petal Width", ticksuffix= "cm") #axis label + format
)
The rbokeh package

• Bokeh is a visualization package that provides a


similar set of interactive features as Plotly (including
hover tooltips, drag-to-pan, and box zoom effects).
• Originally developed for the Python programming
language, Bokeh can be used in R through the
rbokeh package.
#CreateaninteractiveplotoftheirisdatasetusingBokeh
figure(
data =iris, #data for the figure
x_axis(
title="Iris Data Set Visualization" #title for the label="SepalWidth", #label for the
figure axis
)%>% number_formatter= "printf",
#formatter for each axis tick
ly_points(
format= "%scm", #specify the
Sepal.Width, #column for the x- desired tick labeling
axis(withoutquotes!) )%>%
Petal.Width, #column for the y-axis(withoutquotes!) y_axis(
label="PetalWidth", #label for the
color=Species#column for the color encoding
axis
(withoutquotes!)
number_formatter= "printf",
)%>% #formatter for each axis tick
format= "%scm", #specify the
desired tick labeling
)
The Leaflet Package
• Leaflet is an open source JavaScript library for building
interactive maps, which you can use in R through the
leaflet package.
• Maps built with Leaflet have rich interactivity by
default, including the ability to pan, zoom, hover, and
click on map elements and markers.
• They can also be customized to support formatted
labels or respond to particular actions. Indeed, many of
the interactive maps you see accompanying online
news articles are created using Leaflet.
• library(leaflet)
• leaflet() %>%
• addProviderTiles("CartoDB.Positron") %>%
• setView(lng =-122.3321, lat = 47.6062 , zoom = 10 ) #
center the map on Seattle
Shiny App
Shiny is an R package that simplifies the process of building web
applications directly from R code. It provides a framework for
creating interactive web applications without requiring expertise
in web development technologies such as HTML, CSS, or
JavaScript.
It contains basic two components:
1.User Interface (UI): The UI component shows a blueprint for
defining the visual elements of the Shiny app using R functions
that generate HTML.

2.Server: It reacts responsively to user input, processes data, and


generates dynamic output predicated on user interactions.
Shiny

Syntax:
library(shiny)
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
library(shiny)
library(plotly) # Define server logic
# Define UI server <- function(input, output) {
ui <- fluidPage( output$plot <- renderPlotly({
titlePanel("Interactive Plot with mtcars # Create plotly plot based on user
Dataset"), inputs
sidebarLayout( plot_ly(data = mtcars, x =
sidebarPanel( ~get(input$x_var), y =
selectInput("x_var", "X-axis ~get(input$y_var),
variable:", choices = names(mtcars)),
type = 'scatter', mode =
selectInput("y_var", "Y-axis
variable:", choices = names(mtcars), 'markers') %>%
selected = "mpg") layout(title = 'Interactive Plot with
), mtcars Dataset',
mainPanel( xaxis = list(title = input$x_var),
plotlyOutput("plot") yaxis = list(title = input$y_var))
) })
) }
)
# Run the application
shinyApp(ui = ui, server = server)

You might also like