Learn R Programming

LCPA (version 1.0.0)

plotResponse: Visualize Response Distributions with Density Plots

Description

Creates a publication-quality density plot showing the distribution of responses across multiple items/variables. Automatically handles variable ordering, color scaling, and legend layout based on the number of variables.

Usage

plotResponse(response)

Value

A ggplot object containing:

  • Density curves for each variable colored by item

  • Adaptive color schemes based on number of variables

  • Optimized legend layout for large numbers of items

  • Publication-ready theme with grid lines and clean styling

The plot can be further customized using standard ggplot2 syntax.

Arguments

response

A matrix or data frame containing response data where:

  • Rows represent respondents, samples or observations

  • Columns represent variables, items or questions (must have numeric suffixes, e.g., "item1", "Q2")

  • Cell values contain numeric responses

Non-numeric columns (except row identifiers) will cause errors.

Theming Details

The plot uses a minimal theme with:

  • Light grey grid lines for readability

  • Black axis lines and ticks (0.8pt thickness)

  • White background with no panel border

  • Optimized font sizes (13pt axis titles, 11pt tick labels)

  • Legend positioned on right with adaptive sizing

Examples

Run this code
# Simulate response data for 5 items
set.seed(42)
resp_data <- data.frame(
  item1 = rnorm(100, mean = 3, sd = 1),
  item2 = rnorm(100, mean = 2, sd = 0.8),
  item3 = rnorm(100, mean = 4, sd = 1.2),
  item4 = rnorm(100, mean = 3.5, sd = 0.9),
  item5 = rnorm(100, mean = 2.5, sd = 1.1)
)

library(LCPA)
# Generate and display plot
p <- plotResponse(resp_data)
print(p)

# For data with many items (18 items example)
many_items <- as.data.frame(replicate(18, rnorm(50, mean = runif(1, 1, 5), sd = 1)))
names(many_items) <- paste0("Q", 1:18)
p_large <- plotResponse(many_items)
print(p_large)

Run the code above in your browser using DataLab