Creates lollipop charts for categorical data visualization following R Graph Gallery best practices, with emphasis on clinical applications like patient timelines, treatment outcomes, and biomarker comparisons. Uses geom_segment() and geom_point() for optimal visual presentation.
Usage
lollipop(
data,
dep,
group,
useHighlight = FALSE,
highlight,
sortBy = "original",
orientation = "vertical",
showValues = FALSE,
showMean = FALSE,
colorScheme = "default",
theme = "default",
pointSize = 3,
lineWidth = 1,
lineType = "solid",
baseline = 0,
conditionalColor = FALSE,
colorThreshold = 0,
xlabel,
ylabel,
title,
width = 800,
height = 600
)
Arguments
- data
The data as a data frame.
- dep
The numeric variable for the values (lollipop heights/lengths).
- group
The categorical variable for grouping (lollipop categories).
- useHighlight
Enable or disable highlighting of specific levels in the plot.
- highlight
Specific level to highlight in the plot with different color/style.
- sortBy
How to sort the lollipops in the chart.
- orientation
Chart orientation (vertical or horizontal lollipops).
- showValues
Whether to display value labels on the lollipops.
- showMean
Whether to display a reference line at the mean value.
- colorScheme
Color scheme for the lollipops.
- theme
Overall theme/appearance of the plot.
- pointSize
Size of the lollipop points.
- lineWidth
Width of the lollipop stems.
- lineType
Type of line for lollipop stems.
- baseline
Starting point for lollipop stems (default is 0).
- conditionalColor
Enable coloring based on value thresholds.
- colorThreshold
Threshold value for conditional coloring (values above/below get different colors).
- xlabel
Custom label for the x-axis.
- ylabel
Custom label for the y-axis.
- title
Custom title for the plot.
- width
Width of the plot in pixels.
- height
Height of the plot in pixels.
Value
A results object containing:
results$todo | a html | ||||
results$summary | a table | ||||
results$plot | an image |
Tables can be converted to data frames with asDF
or as.data.frame
. For example:
results$summary$asDF
as.data.frame(results$summary)
Examples
# Load clinical lab data
clinical_lab_data <- read.csv("clinical_lab_data.csv")
#> Warning: cannot open file 'clinical_lab_data.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection
# Or load from package: data("clinical_lab_data", package = "ClinicoPath")
# Basic lollipop chart - Hemoglobin by treatment group
lollipop(
data = clinical_lab_data,
dep = "hemoglobin",
group = "treatment_group",
sortBy = "value_desc",
title = "Hemoglobin Levels by Treatment"
)
#> Error: object 'clinical_lab_data' not found
# Highlighting severe disease cases - Albumin levels
lollipop(
data = clinical_lab_data,
dep = "albumin",
group = "disease_severity",
useHighlight = TRUE,
highlight = "Severe",
orientation = "horizontal",
showValues = TRUE,
colorScheme = "clinical",
title = "Albumin Levels by Disease Severity"
)
#> Error: object 'clinical_lab_data' not found
# Conditional coloring for abnormal creatinine (>1.2 mg/dL)
lollipop(
data = clinical_lab_data,
dep = "creatinine",
group = "age_group",
conditionalColor = TRUE,
colorThreshold = 1.2,
lineType = "dashed",
sortBy = "value_asc",
title = "Creatinine by Age (Threshold: 1.2 mg/dL)"
)
#> Error: object 'clinical_lab_data' not found
# Advanced - Platelet count with clinical baseline
lollipop(
data = clinical_lab_data,
dep = "platelet_count",
group = "hospital",
baseline = 150, # Lower normal limit
useHighlight = TRUE,
highlight = "Hospital A",
lineType = "dotted",
pointSize = 4,
showMean = TRUE,
colorScheme = "clinical",
title = "Platelet Counts by Hospital (Normal >150)"
)
#> Error: object 'clinical_lab_data' not found
# Compare WBC across multiple factors
lollipop(
data = clinical_lab_data,
dep = "white_blood_cells",
group = "treatment_group",
conditionalColor = TRUE,
colorThreshold = 11, # Upper normal limit
orientation = "horizontal",
sortBy = "value_desc",
lineWidth = 2,
title = "WBC Count by Treatment (ULN: 11)"
)
#> Error: object 'clinical_lab_data' not found