Title: | The Corset Plot |
---|---|
Description: | Corset plots are a visualization technique used strictly to visualize repeat measures at 2 time points (such as pre- and post- data). The distribution of measurements are visualized at each time point, whilst the trajectories of individual change are visualized by connecting the pre- and post- values linearly. These lines can be coloured to represent the magnitude of change, or other user-defined value. This method of visualization is ideal for showing the heterogeneity of data, including differences by sub-groups. The package relies on 'ggplot2' allowing for easy integration so that users can customize their visualizations as required. Users can create corset plots using data in either wide or long format using the functions gg_corset() or gg_corset_elongated(), respectively. |
Authors: | Kyla Belisario |
Maintainer: | Kyla Belisario <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.0 |
Built: | 2025-03-05 03:01:36 UTC |
Source: | https://github.com/kbelisar/ggcorset |
An example data set from simulated data.
data(drinkdays)
data(drinkdays)
An object of class data.frame
with 300 rows and 3 columns.
## Not run: data(drinkdays) ## End(Not run)
## Not run: data(drinkdays) ## End(Not run)
This function visualizes a corset plot in wide format.
data |
The name of the data frame. |
y_var1 |
The name of measured variable at time 1. |
y_var2 |
The name of measured variable at time 2. |
group |
The name of units measured at each time point such as 'ID'. The trajectories of these units are visualized by the lines of the corset plot. |
c_var |
The name of variable to visualize by line colour, such as percent change, magnitude of change, or direction of change. |
eyelets |
Optional (default is FALSE). If set to true, this will visualize one of two mean types by c_var, as defined by the 'e_type' argument. |
e_type |
Optional eyelet type if the eyelets parameter is set to TRUE. One of "SE" or "SD". The default is standard error ("SE") means. Alternatively, standard deviations ("SD") with means can be specified, which include horizontal lines to denote +1 and -1 standard deviation. Note that the visualization of standard deviations works best in tandem with the faceted option. |
faceted |
Optional (default is FALSE). If set to true, the c_var will be faceted. |
facet_design |
Optional facet type when the faceted parameter is set to TRUE. One of "original","group", or "line". The default is "original", which provides facets void of any special features. The "group" option includes the overall distribution of the entire sample in the background of each facet (which defaults to the 'vio_fill' colour), alongside each distribution for each c_var group. The "line" option includes all individual trajectories in the background of each facet using a soft grey (default) or custom colour as chosen by 'line_col' argument. |
vio_fill |
Optional (defaults to a soft black). Use to change the fill colour of the half violins. |
line_size |
Optional. Use to change the size (thickness) of the lines which visualize change for each unit identified by the group variable. Default is 0.25. |
line_col |
Optional custom colour of the background individual lines when the facet_design is set to "line". Defaults to a soft grey. |
line_dodge |
Optional. Use to change the amount of vertical dodge of the lines which visualize each unit of the group variable. Default is 0.1. |
ggplot2 graphical object
wide.df <- data.frame(id = c(1:20), time1 = c(3,4,7,5,6,3,4,1,7,0,5,2,0,1,6,2,1,7,4,6), time2 = c(5,5,7,3,0,3,3,2,7,0,3,4,3,3,7,0,0,6,5,6)) wide.df$change <- wide.df$time2-wide.df$time1 wide.df$direction <- ifelse(wide.df$change==0,"No Change", ifelse(wide.df$change>0,"Increase","Decrease")) gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "change") ## Create corset plots with eyelets: gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "direction", eyelets = TRUE) ## Create faceted corset plots based on direction of change: gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "direction", faceted = TRUE) ## Create faceted corset plots with standard deviation eyelets: gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "direction", e_type = "SD", faceted = TRUE)
wide.df <- data.frame(id = c(1:20), time1 = c(3,4,7,5,6,3,4,1,7,0,5,2,0,1,6,2,1,7,4,6), time2 = c(5,5,7,3,0,3,3,2,7,0,3,4,3,3,7,0,0,6,5,6)) wide.df$change <- wide.df$time2-wide.df$time1 wide.df$direction <- ifelse(wide.df$change==0,"No Change", ifelse(wide.df$change>0,"Increase","Decrease")) gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "change") ## Create corset plots with eyelets: gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "direction", eyelets = TRUE) ## Create faceted corset plots based on direction of change: gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "direction", faceted = TRUE) ## Create faceted corset plots with standard deviation eyelets: gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "direction", e_type = "SD", faceted = TRUE)
This function visualizes a corset plot in long format.
data |
The name of the data frame. |
x_var |
The name of the x_axis variable. |
x_vals |
The values of the two time points. |
y_var |
The repeated measure variable name. |
group |
The name of units measured at each time point such as 'ID'. The trajectories of these units are visualized by the lines of the corset plot. |
c_var |
The name of variable to visualize by line colour, such as percent change. |
eyelets |
Optional (default is FALSE). If set to true, this will visualize one of two mean types by c_var, as defined by the 'e_type' argument. |
e_type |
Optional eyelet type if the eyelets parameter is set to TRUE. One of "SE" or "SD". The default is standard error ("SE") means. Alternatively, standard deviations ("SD") with means can be specified, which include horizontal lines to denote +1 and -1 standard deviation. Note that the visualization of standard deviations works best in tandem with the faceted option. |
faceted |
Optional (default is FALSE). If set to true, the c_var will be faceted. |
facet_design |
Optional facet type when the faceted parameter is set to TRUE. One of "original","group", or "line". The default is "original", which provides facets void of any special features. The "group" option includes the overall distribution of the entire sample in the background of each facet (which defaults to the 'vio_fill' colour), alongside each distribution for each c_var group. The "line" option includes all individual trajectories in the background of each facet using a soft grey (default) or custom colour as chosen by 'line_col' argument. |
vio_fill |
Optional (defaults to a soft black). Use to change the fill colour of the half violins. |
line_size |
Optional. Use to change the size (thickness) of the lines which visualize change for each unit identified by the group variable. Default is 0.25. |
line_col |
Optional custom colour of the background individual lines when the facet_design is set to "line". Defaults to a soft grey. |
line_dodge |
Optional. Use to change the amount of vertical dodge of the lines which visualize each unit of the group variable. Default is 0.1. |
ggplot2 graphical object
long.df <- data.frame(id = c(rep(1:20,2)), time = c(rep(c("pre","post"),each = 20)), days = c(3,4,7,5,6,3,4,1,7,0,5,2,0,1,6,2,1,7,4,6, 5,5,7,3,0,3,3,2,7,0,3,4,3,3,7,0,0,6,5,6), change = c(2,1,0,-2,-6,0,-1,1,0,0,-2,2,3,2,1,-2,-1,-1,1,0, 2,1,0,-2,-6,0,-1,1,0,0,-2,2,3,2,1,-2,-1,-1,1,0)) long.df$direction <- ifelse(long.df$change==0,"No Change", ifelse(long.df$change>0,"Increase","Decrease")) gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "change") ## Create groupings based on direction of change to use for eyelets: gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "direction", eyelets = TRUE) ## Create faceted corset plots based on direction of change: gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "direction", faceted = TRUE) ## Create faceted corset plots with standard deviation eyelets: gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "direction", e_type = "SD", faceted = TRUE)
long.df <- data.frame(id = c(rep(1:20,2)), time = c(rep(c("pre","post"),each = 20)), days = c(3,4,7,5,6,3,4,1,7,0,5,2,0,1,6,2,1,7,4,6, 5,5,7,3,0,3,3,2,7,0,3,4,3,3,7,0,0,6,5,6), change = c(2,1,0,-2,-6,0,-1,1,0,0,-2,2,3,2,1,-2,-1,-1,1,0, 2,1,0,-2,-6,0,-1,1,0,0,-2,2,3,2,1,-2,-1,-1,1,0)) long.df$direction <- ifelse(long.df$change==0,"No Change", ifelse(long.df$change>0,"Increase","Decrease")) gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "change") ## Create groupings based on direction of change to use for eyelets: gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "direction", eyelets = TRUE) ## Create faceted corset plots based on direction of change: gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "direction", faceted = TRUE) ## Create faceted corset plots with standard deviation eyelets: gg_corset_elongated(data = long.df, x_var = "time", x_vals = c("pre","post"), y_var = "days", group = "id", c_var = "direction", e_type = "SD", faceted = TRUE)
This function offers a ggplot theme to make visualizations more polished.
theme_ggcorset()
theme_ggcorset()
ggplot2 theme
wide.df <- data.frame(id = c(1,2,3,4,5), time1 = c(3,4,7,5,6), time2 = c(5,5,7,3,0), change = c(28.57,14.29,0,-28.57,-85.71)) plot1 <- gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "change") plot1 + theme_ggcorset()
wide.df <- data.frame(id = c(1,2,3,4,5), time1 = c(3,4,7,5,6), time2 = c(5,5,7,3,0), change = c(28.57,14.29,0,-28.57,-85.71)) plot1 <- gg_corset(data = wide.df, y_var1 = "time1", y_var2 = "time2", group = "id", c_var = "change") plot1 + theme_ggcorset()