This repository houses R scripts for data visualization. The point of these scripts is not to present meaningful data analyses, but simply to provide examples of common data visualization tools. All scripts are written in R, using tidyverse, ggplot2, and reshape2 libraries.
Code File: ggplot_boxplot.R
Video Tutorial: https://youtu.be/1BMa5X5eruQ
Description: This script provides a simple example of a grouped box plot (goals scored by NHL players during the 2018 season, grouped by position). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, changing column data types within a data.frame, and string location and replacement.
The code also includes a call to geom_jitter(), also grouped by position, which plots individual data points over the boxplots.
Code File: ggplot_point_boxplot_combo.R
Video Tutorial: https://www.youtube.com/watch?v=lIWE_7OS9N4&t=32s
Description: This script provides an example of grouped boxplots and connected points on a single plot. This type of figure can be used, for example, to show group- and individual-level responses to an intervention:
Code File: ggplot_violin.R
Description: This script provides examples of a grouped violin plots, similar to the box plots above (goals scored by NHL players during the 2018 season, grouped by position). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, changing column data types within a data.frame, and string location and replacement.
The code also includes a call to geom_boxplot(), also grouped by position, which adds box plots to each violin.
Code File: ggplot_grouped_continuous_data.R
Data File: data/continuous_grf_curves.xlsx
Video Tutorial: https://www.youtube.com/watch?v=rQZ8u1cbycg&t=1s
Description: This script provides a simple example of a grouped line plot, showing the mean and standrad deviation (as colored ribbons) of a continuous variable for three separate groups. The continuous variable in this example is the anteroposterior ground reaction force captured during a dynamic postural disturbance.
Code File: ggplot_lineplot.R
Description: This script provides a simple example of a line plot (high temperature across days of September in Birmingham, AL). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, and changing column data types within a data.frame.
Code File: ggplot_scatterplot.R
Video Tutorial: https://youtu.be/XEgyC1Oz9KQ
Description: This script provides simple examples of scatter plots (goals scored versus time on ice by NHL players). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, changing column data types within a data.frame, and string location and replacement. The script has an example of an ungrouped scatter plot:
The code also includes an example of a grouped scatter plot (grouped by position):
Finally, the code includes a call to geom_smooth(), which adds a best fit line:
Code File: ggplot_density2d.R
Video Tutorial: https://youtu.be/_eDzjQXaU9s
Description: This script provides simple examples of filled and unfilled density plots with jittered data overlayed. Here is an example of a filled density plot:
Code File: ggplot_marginal_distributions.R
Video Tutorial: https://www.youtube.com/watch?v=NApff-foQn4&feature=youtu.be
Description: This script provides a simple examples of a scatter plot with histograms plotted in the margins. Note that the video tutorial also includes instructions for adding density and box plots to the margins.
Code File: ggplot_histogram.R
Video Tutorial: https://youtu.be/Iax_qtK1XxM
Description: This script provides simple examples of histograms (goals scored in a season, across multiple seasons). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, changing column data types within a data.frame, and string location and replacement. The script has an example of an ungrouped histogram:
The code also includes an example of a grouped histogram (grouped by position, forward versus defense). Overlapping distributions can be shown as completely overlapping, dodged, or transparent:
Finally, the code includes a call to geom_density(), which includes a density distribution:
Code File: ggplot_cormat.R
Video Tutorial: https://youtu.be/W9YMjvlRtCg
Description: This script provides an example of a heat mapped correlation matrix (from a large NHL dataset). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, changing column data types within a data.frame, string location and replacement, and generating a correlation matrix. The script has an example of a simple matrix:
The code also includes an example that plots the correlation coefficient in text over each correlation matrix position:
Code File: ggplot_ridgeplot.R
Video Tutorial: https://youtu.be/3fuzBLlft9Q
Description: This script provides a simple example of a grouped ridgeline plot (average avocado prices across months in 2019 grouped by month). The tidy portion of the code also provides useful information on subsetting data using variable-specific filtering, changing column data types within a data.frame, string location and replacement, and adding column data using mutate().
Code File: ggplot_facetwrap_histos.R
Description: This script provides examples of facet wrapped histograms (distribution of goals within each NHL team during the 2018 season). The tidy portion of the code also provides useful information on subsetting data based on column number and variable-specific filtering, changing column data types within a data.frame, and string location and replacement. The script has an example of a simple facet wrap:
The code also includes examples with added complexity (grouping by a variable within each team and adding density):