Learn R Programming

grafify (version 5.1.0)

plot_xy_Group: Plot points on a quantitative X - Y plot & a grouping variable.

Description

This function takes a data table, quantitative X and Y variables along with a grouping variable that is either categorical or numeric. The colour of data symbols is mapped to the grouping variable. This function is related to plot_xy_CatGroup and plot_xy_NumGroup, and could eventually replace them in future updates.

Central value and scatter can be shown by choosing the ErrorType argument. Mean and error bars (SD, SEM or CI95) or box and whisker plot options are available.

Usage

plot_xy_Group(
  data,
  xcol,
  ycol,
  Group,
  facet,
  ErrorType = c("none", "SD", "SEM", "CI95", "Boxplot"),
  SmoothType = c("none", "Loess", "Linear"),
  symsize = 3,
  s_alpha,
  TextXAngle = 0,
  mean_size,
  m_alpha = 1,
  LogYTrans,
  LogXTrans,
  LogYBreaks = waiver(),
  LogXBreaks = waiver(),
  LogYLabels = waiver(),
  LogXLabels = waiver(),
  LogYLimits = NULL,
  LogXLimits = NULL,
  facet_scales = "fixed",
  fontsize = 20,
  bwid = 0.3,
  b_alpha = 0.3,
  l_alpha = 0.8,
  sm_alpha = 0.3,
  symthick,
  bthick,
  ewid = 0.1,
  e_alpha = 1,
  ColPal = NULL,
  ColSeq = TRUE,
  ColRev = FALSE,
  ...
)

Value

This function returns a ggplot2 object of class "gg" and "ggplot".

Arguments

data

A data frame containing the variables to be plotted.

xcol

A column name in data for the x-axis (typically a factor or grouping variable).

ycol

A column name in data for the y-axis (numeric).

Group

A grouping variable used for colour/fill aesthetics. Whether this variable is numeric or not will determine colour palette choice for the ColPal argument.

facet

An optional variable for faceting the plot using facet_wrap().

ErrorType

select the way to show data centrality and dispersion. The default is "none", which can be changed to "SD" (standard deviation), "SEM" (standard error of the mean) or "CI95" (95% confidence interval based on t distributions); all these will be displayed with a square symbol representing the mean. Choosing Boxplot will show a box and whiskers plot and the median. A line joining the central values will also appear. Use l_alpha = 0 to not show the line.

SmoothType

Add a smoothened average using 'Loess' or 'Linear', which will be passed on to stat_smooth as method = "loess" or method = "lm", respectively.

symsize

Size of the raw data points. Default is 3.

s_alpha

Alpha transparency for raw data points. Default is 0.8, which will reduce to 0.2 when an ErrorType is set.

TextXAngle

Angle of x-axis text labels. Default is 0.

mean_size

Size of the square symbol representing the mean. Default is symsize + 3 to prominently show the central value.

m_alpha

Alpha transparency for the mean symbol. Default is 1.

LogYTrans

transform Y axis into "log10" or "log2" (in quotes).

LogXTrans

transform X axis into "log10" or "log2"

LogYBreaks

argument for ggplot2[scale_y_continuous] for Y axis breaks on log scales, default is waiver(), or provide a vector of desired breaks.

LogXBreaks

argument for ggplot2[scale_x_continuous] for Y axis breaks on log scales, default is waiver(), or provide a vector of desired breaks.

LogYLabels

argument for ggplot2[scale_y_continuous] for Y axis labels on log scales, default is waiver(), or provide a vector of desired labels.

LogXLabels

argument for ggplot2[scale_x_continuous] for Y axis labels on log scales, default is waiver(), or provide a vector of desired labels.

LogYLimits

a vector of length two specifying the range (minimum and maximum) of the Y axis.

LogXLimits

a vector of length two specifying the range (minimum and maximum) of the X axis.

facet_scales

whether or not to fix scales on X & Y axes for all graphs. Can be fixed (default), free, free_y or free_x (for Y and X axis one at a time, respectively).

fontsize

parameter of base_size of fonts in theme_classic, default set to size 20.

bwid

width of boxplot (default = 0.3).

b_alpha

fractional opacity of boxes (default = 0.3).

l_alpha

fractional opacity of lines joining boxes, (default = 0.8).

sm_alpha

fractional opacity of error range around loess or linear smooth fit (default = 0.3).

symthick

size (in 'pt' units) of outline of symbol lines (stroke), default = fontsize/22.

bthick

size (in 'pt' units) of outline of boxes, whisker and joining lines (stroke), default = fontsize/22.

ewid

width of errorbars (default = 0.1).

e_alpha

fractional opacity of error bars (default = 1).

ColPal

Character. Name of the colour palette to use from grafify. For categorical variables, one of: "okabe_ito", "all_grafify", "bright", "contrast", "dark", "fishy", "kelly", "light", "muted", "pale", "r4", "safe", "vibrant". For quantitative variables, one of: blue_conti, yellow_conti, grey_conti, PrGn_div, PrGn_div.

ColSeq

logical TRUE or FALSE. Default TRUE for sequential colours from chosen palette. Set to FALSE for distant colours, which will be applied using scale_fill_grafify2.

ColRev

whether to reverse order of colour within the selected palette, default F (FALSE); can be set to T (TRUE).

...

Additional arguments passed to ggplot2 geoms or scales.

Details

When ErrorType is set to a value other than none, a line joining the central value (mean or median, respectively) will also appear (set l_alpha = 0 if this is not desired). Other options for ErrorType are: SD, SEM, CI95, Boxplot.

When SD/SEM/CI95 are chosen, the mean value will appear as a larger square symbol. Its opacity and size can be adjusted with m_alpha and mean_size, respectively.

The smooth fitted line type can be shown with the SmoothType argument, which can take the following options: none, Linear or Loess. Latter two options are fitted using stat_smooth with lm or loess options, respectively.

Colours can be changed using ColPal, ColRev or ColSeq arguments. Colours available can be seen quickly with plot_grafify_palette. ColPal can be one of the following: "okabe_ito", "dark", "light", "bright", "pale", "vibrant, "muted" or "contrast". ColRev (logical TRUE/FALSE) decides whether colours are chosen from first-to-last or last-to-first from within the chosen palette. ColSeq (logical TRUE/FALSE) decides whether colours are picked by respecting the order in the palette or the most distant ones using colorRampPalette.

Examples

Run this code
#The grouping factor cyl  is automatically converted to categorical variable
plot_xy_Group(data = mpg,
xcol = cyl, ycol = cty, Group = drv,
ErrorType = "SD")

#with a Linear smooth line
plot_xy_Group(data = mpg,
xcol = cyl, ycol = cty, Group = drv,
ErrorType = "SD", SmoothType = "Linear")
 

Run the code above in your browser using DataLab