View source: R/symmetry_plot.R
symmetry_plot | R Documentation |
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"
)
df |
The target data frame from which the distance from the median are plotted. |
var_name |
A string that sets the variable name of interest from 'df'. This is a required parameter. |
position |
A string or function that does a slight adjustment to overlapping points. Typical values are
"jitter" or |
title |
A string that sets the plot title. |
subtitle |
A string that sets the plot subtitle. |
caption |
A string that sets the plot caption |
center_titles |
A logical which if |
x_title |
A string that sets the x axis title. If |
y_title |
A string that sets the y axis title. If |
hide_x_tics |
A logical that controls the appearance of the x axis tics. |
hide_y_tics |
A logical that controls the appearance of the y axis tics. |
rot_x_tic_angle |
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. |
rot_y_tic_label |
A logical which if |
x_limits |
Sets the minimum and maximum for the x axis. |
x_major_breaks |
A numeric vector or function that defines the exact major tic locations along the x axis. |
x_minor_breaks |
A numeric vector or function that defines the exact minor tic locations along the x axis. |
x_labels |
A character vector with the same length as 'x_major_breaks', that labels the major tics. |
y_limits |
A numeric 2 element vector that sets the minimum and maximum for the y axis. |
y_major_breaks |
A numeric vector or function that defines the exact major tic locations along the y axis. |
y_minor_breaks |
A numeric vector or function that defines the exact minor tic locations along the y axis. |
y_labels |
A character vector with the same length as 'y_major_breaks', that labels the major tics. |
x_y_decimals |
A two element numeric vector that set the number of decimals for the x and y tic labels. |
x_y_scientific |
A two element logical vector that if |
axis_text_size |
A numeric that sets the font size along the axis'. Default is 11. |
pts_fill |
A string that sets the fill color attribute of the points. |
pts_shape |
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". |
pts_stroke |
A numeric that sets the drawing stroke width attribute for a point shape. |
pts_color |
A string that sets the color attribute of the points. |
pts_size |
A numeric value that sets the size attribute of the points. |
pts_alpha |
A numeric value that sets the alpha level attribute of points. |
line_width |
A numeric value that sets the width of the symmetry line. |
line_color |
A string that sets the color of the symmetry line. |
line_alpha |
A numeric that sets the alpha of the symmetry line. |
line_linetype |
A string that sets the symmetry line type "twodash", "solid", "longdash", "dotted", "dotdash", "dashed", "blank". |
show_major_grids |
A logical that controls the appearance of major grids. |
show_minor_grids |
A logical that controls the appearance of minor grids. |
panel_color |
A string in hexidecimal or color name that sets the plot panel's color. The default is "white". |
panel_border_color |
A string in hexidecimal or color name that sets the plot panel's border color. The default is "black". |
A ggplot class plot object
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"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.