Function creates a ggplot2 based symmetry plot from a vector of numeric values.
Function orders the numeric vector from low to high. It then divides the data into two groups: values above the median(u group) and values below the median(v group). The distances from the median for values in each group are computed. A scatter plot is created where distances in the v group are plotted along the x axis and distances from the v group are plotted along the y axis. If the upper and lower distance data forms a straight line then the original data is perfectly symmetrical.
A reference symmetry line is also drawn. If points fall close to the line then the data is nearly symmetric. If the data is left-skewed, then the points fall below the line. If the data is right skewed, the points fall above the line.
Function returns a ggplot2 plot object of x/y scatter point distances from the median. Options are provided for axis scaling and point labeling.
symmetry_plot(
df,
var_name,
position = position_jitter(width = 0, height = 0),
title = NULL,
subtitle = NULL,
caption = NULL,
center_titles = FALSE,
x_title = "V",
y_title = "U",
hide_x_tics = FALSE,
hide_y_tics = FALSE,
rot_x_tic_angle = 0,
rot_y_tic_label = FALSE,
x_limits = NULL,
x_major_breaks = waiver(),
x_minor_breaks = waiver(),
x_labels = waiver(),
y_limits = NULL,
y_major_breaks = waiver(),
y_minor_breaks = waiver(),
y_labels = waiver(),
x_y_decimals = NULL,
x_y_scientific = NULL,
axis_text_size = 11,
pts_fill = "black",
pts_shape = 21,
pts_stroke = 1,
pts_color = "black",
pts_size = 1,
pts_alpha = 1,
line_width = 0.6,
line_color = "blue",
line_alpha = 0.7,
line_linetype = "solid",
show_major_grids = TRUE,
show_minor_grids = TRUE,
panel_color = "white",
panel_border_color = "black"
)
A ggplot class plot object
The target data frame from which the distance from the median are plotted.
A string that sets the variable name of interest from 'df'. This is a required parameter.
A string or function that does a slight adjustment to overlapping points. Typical values are
"jitter" or position_jitter(width = 0.1, height = 0.1)
.
A string that sets the plot title.
A string that sets the plot subtitle.
A string that sets the plot caption
A logical which if TRUE
centers both the 'title' and 'subtitle'.
A string that sets the x axis title. If NULL
(the default) then the x axis title does not appear.
A string that sets the y axis title. If NULL
(the default) then the y axis title does not appear.
A logical that controls the appearance of the x axis tics.
A logical that controls the appearance of the y axis tics.
A numeric that sets the angle of rotation for the x tic labels. When x tic labels are long, a value of 40 for this argument usually works well.
A logical which if TRUE
rotates the y tic labels 90 degrees for enhanced readability.
Sets the minimum and maximum for the x axis.
A numeric vector or function that defines the exact major tic locations along the x axis.
A numeric vector or function that defines the exact minor tic locations along the x axis.
A character vector with the same length as 'x_major_breaks', that labels the major tics.
A numeric 2 element vector that sets the minimum and maximum for the y axis.
A numeric vector or function that defines the exact major tic locations along the y axis.
A numeric vector or function that defines the exact minor tic locations along the y axis.
A character vector with the same length as 'y_major_breaks', that labels the major tics.
A two element numeric vector that set the number of decimals for the x and y tic labels.
A two element logical vector that if TRUE
uses scientific notation for the x and y tic labels.
A numeric that sets the font size along the axis'. Default is 11.
A string that sets the fill color attribute of the points.
A numeric integer that sets the shape attribute of the points. Typical values are 21 "circle", 22 "square", 23 "diamond", 24 "up triangle", 25 "down triangle".
A numeric that sets the drawing stroke width attribute for a point shape.
A string that sets the color attribute of the points.
A numeric value that sets the size attribute of the points.
A numeric value that sets the alpha level attribute of points.
A numeric value that sets the width of the symmetry line.
A string that sets the color of the symmetry line.
A numeric that sets the alpha of the symmetry line.
A string that sets the symmetry line type "twodash", "solid", "longdash", "dotted", "dotdash", "dashed", "blank".
A logical that controls the appearance of major grids.
A logical that controls the appearance of minor grids.
A string in hexidecimal or color name that sets the plot panel's color. The default is "white".
A string in hexidecimal or color name that sets the plot panel's border color. The default is "black".
library(ggplot2)
library(RplotterPkg)
RplotterPkg::symmetry_plot(
df = RplotterPkg::farms,
var_name = "count",
title = "Symmetry in farm counts across US states",
rot_y_tic_label = TRUE,
line_linetype = "dotted"
)
Run the code above in your browser using DataLab