MIT 302 - Statistical Computing II - Tutorial 04
MIT 302 - Statistical Computing II - Tutorial 04
MIT 302 - Statistical Computing II - Tutorial 04
COMPUTING 11
TUTORIAL 4: Data Visualization with R
1 Overview
Data visualization is a crucial aspect of statistical computing as it allows us to explore and
communicate patterns, relationships, and insights in our data effectively. R provides a wide
range of powerful packages and functions for creating various types of visualizations, from
basic plots to complex and interactive graphics.
1.1 Basic Plots
R offers several functions for creating basic plots, including:
• Scatter plots: Use the "plot" function to visualize the relationship between two numeric
variables.
• Line plots: Use the "plot" or "lines" function to display trends or time series data.
• Bar plots: Use the "barplot" function to compare categorical variables.
• Histograms: Use the "hist" function to visualize the distribution of a numeric variable.
1.2 Advanced Plots
R provides packages like "ggplot2" that offer more flexibility and customization for creating
advanced plots. The "ggplot2" package follows the grammar of graphics approach, allowing
you to build plots layer by layer.
• Box plots: Use the "geom_boxplot" function to create box plots, which display the
distribution of a numeric variable across different categories.
• Violin plots: Use the "geom_violin" function to create violin plots, which combine a
box plot with a kernel density plot.
• Heatmaps: Use functions like "heatmap" or "geom_tile" in "ggplot2" to visualize the
patterns and relationships in a matrix of values.
• Scatterplot matrices: Use the "pairs" function or the "ggpairs" function from the
"GGally" package to create scatterplot matrices for exploring multiple variables.
1.3 Interactive and Dynamic Visualizations
R provides packages like "plotly" and "shiny" for creating interactive and dynamic
visualizations.
• Interactive plots: Use the "plot_ly" function from the "plotly" package to create
interactive plots that allow zooming, panning, and tooltips.
• Dashboards and web applications: Use the "shiny" package to build interactive web
applications and dashboards that update dynamically based on user inputs.
1.4 Geographic Visualizations
R has packages like "ggplot2" and "leaflet" for creating geographic visualizations.
• Choropleth maps: Use the "geom_map" function in "ggplot2" or the "addPolygons"
function in "leaflet" to create choropleth maps, which represent data using different
colors on a map.
• Interactive maps: Use the "leaflet" package to create interactive and customizable maps
with various layers, markers, and pop-ups.
1.5 Customizing Plots
R allows you to customize plots by modifying aspects like labels, titles, axes, colors, and
themes.
• Labels and titles: Use functions like "labs" in "ggplot2" to customize plot labels and
titles.
• Axes and scales: Use functions like "scale_x_continuous" or "scale_x_discrete" in
"ggplot2" to modify the appearance of axes and scales.
• Colors and themes: Use functions like "scale_color_manual" or "theme" in "ggplot2"
to customize colors and themes of your plots.
R's extensive ecosystem of packages, along with its flexibility and programmability, makes it
a powerful tool for data visualization. By combining data visualization with statistical analysis,
you can gain deeper insights and effectively communicate your findings.
# Define the UI
ui <- fluidPage(
titlePanel("Interactive Scatter Plot"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "cylinders",
label = "Number of Cylinders",
min = min(mtcars$cyl),
max = max(mtcars$cyl),
value = min(mtcars$cyl),
step = 1)
),
mainPanel(
plotOutput(outputId = "scatterPlot")
)
)
)