library(data.table)
library(ggplot2)
library(magrittr)
library(devtools)
library(socviz)
library(RColorBrewer)
library(here)
Demos for deandevl/RplotterPkg
The following scripts are a sampling of 2 plots from the package’s 46 demos.
Load the required R Packages from CRAN:
Load the package deandevl/RplotterPkg
from github.
::install_github('deandevl/RplotterPkg') devtools
RplotterPkg::create_bar_plot
The first plot is a bar chart with a dataset of 2867 respondents from the General Social Survey, 2016 (socviz::gss_sm
package). The variables are religion (six possible character categories of religion) and happy (3 levels of “Very Happy”, “Pretty Happy”, “Not Too Happy”). data.table techniques are used to group the dataset by both variables and calculate a percentage of “happy” for each religion.
Create a group bar chart:
<- data.table::as.data.table(socviz::gss_sm) %>%
religion_happy_percent_dt !is.na(religion) & !is.na(happy)] %>%
.[N = .N), by = .(happy, religion)] %>%
.[, .(happy = happy, N = N, Total = sum(N), Percent = N/sum(N)), by = religion]
.[, .(
::create_bar_plot(
RplotterPkgdf = religion_happy_percent_dt,
aes_x = "religion",
aes_y = "Percent",
aes_fill = "happy",
position = "dodge",
rot_y_tic_label = T,
subtitle = "Happiness and Religion",
caption = "Source: General Social Survey, 2016",
x_title = "Religion",
y_title = "Percent"
)
RplotterPkg::create_heatmap
The second plot deals with baseball batter’s “swing and miss” percentages when we look at a pitcher’s combination of the ball’s spin rate and velocity (see Analyzing Baseball Spin Rate and Training Pitchers).
The dataset consist of three variables: velocity(from 82 to 100 mph), spinrate(from 1600 to 2700 rpm), and swing_miss(from 0 to 20%) read from a CSV file. For the heatmap, velocity will be along the x-axis; spinrate will be along the y-axis; swing_miss will be a cell fill color correlated with its percentage value.
Read in the dataset:
<- file.path(here::here(), "r_packages", "RplotterPkg", "demos", "data", "spinrates.csv")
file_path <- data.table::fread(file = file_path) spinrates_dt
Create the heatmap:
::create_heatmap(
RplotterPkgdf = spinrates_dt,
aes_x = "velocity",
aes_y = "spinrate",
aes_fill = "swing_miss",
aes_label = "swing_miss",
label_fontface = "bold",
title = "Likelihood of swinging and missing on a fastball",
x_title = "Velocity",
y_title = "Spinrate",
rot_y_tic_label = T,
x_limits = c(82,101),
x_major_breaks = seq(82,101,1),
y_limits = c(1500, 2800),
y_major_breaks = seq(1500, 2800, 100)
+
) ::scale_fill_gradientn(
ggplot2colors = RColorBrewer::brewer.pal(n = 9, name = "YlOrRd"),
n.breaks = 8
+
) ::guides(
ggplot2fill = ggplot2::guide_colorbar(
ticks.colour = "black"
) )
The chart shows for an x-y combination cell, the labelled swing_miss percentage and its correlated fill color.