Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions R/plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ data_plot.estimate_density <- function(
if (!"Parameter" %in% names(dataplot)) {
dataplot$Parameter <- "Distribution"
}

# Handle case where Parameter column exists but is empty or malformed
if ("Parameter" %in% names(dataplot)) {
# Check for various problematic conditions with descriptive variables
parameter_is_empty <- length(dataplot$Parameter) == 0
parameter_all_na <- all(is.na(dataplot$Parameter))
parameter_all_empty_strings <- all(dataplot$Parameter == "")
parameter_no_unique_values <- length(unique(dataplot$Parameter[!is.na(dataplot$Parameter)])) == 0

parameter_is_problematic <- parameter_is_empty || parameter_all_na ||
parameter_all_empty_strings || parameter_no_unique_values

if (parameter_is_problematic) {
dataplot$Parameter <- rep("Distribution", nrow(dataplot))
}
}

# add component and effects columns
if (!is.null(data)) {
Expand All @@ -23,11 +39,25 @@ data_plot.estimate_density <- function(

dataplot <- .fix_facet_names(dataplot)

dataplot$Parameter <- factor(dataplot$Parameter)
dataplot$Parameter <- factor(
dataplot$Parameter,
levels = rev(levels(dataplot$Parameter))
)
# Safely convert Parameter to factor, ensuring it has valid data
parameter_column_exists <- "Parameter" %in% names(dataplot)
parameter_has_data <- length(dataplot$Parameter) > 0
parameter_length_matches <- length(dataplot$Parameter) == nrow(dataplot)
parameter_not_all_na <- !all(is.na(dataplot$Parameter))

parameter_is_valid_for_factor <- parameter_column_exists && parameter_has_data &&
parameter_length_matches && parameter_not_all_na

if (parameter_is_valid_for_factor) {
dataplot$Parameter <- factor(dataplot$Parameter)
dataplot$Parameter <- factor(
dataplot$Parameter,
levels = rev(levels(dataplot$Parameter))
)
} else {
# If Parameter column is still problematic, set a default with correct length
dataplot$Parameter <- factor(rep("Distribution", nrow(dataplot)))
}

# summary
split_columns <- intersect(
Expand Down Expand Up @@ -250,6 +280,12 @@ plot.see_estimate_density <- function(
p <- p + facet_wrap(~Component, scales = "free", ncol = n_columns)
}
}

# Handle Group column for grouped data (e.g., from group_by in estimate_density)
# Only add facets if not already faceting by other variables and if we have groups
if ("Group" %in% names(x) && is.null(n_columns)) {
p <- p + facet_wrap(~Group, scales = "free")
}

p
}
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ test_that("`plot.see_estimate_density()` works", {
})


test_that("`plot.see_estimate_density()` works with group_by and vector input", {
skip_if_not_installed("bayestestR")
skip_if_not_installed("vdiffr")

# Test case that was failing: vector input with group_by
df <- bayestestR::estimate_density(iris[c("Species", "Petal.Width")], group_by = "Species")

# This should not error
expect_no_error(p <- plot(df))
expect_s3_class(p, "gg")

# Visual snapshot test
vdiffr::expect_doppelganger(
title = "plot.estimate_density with group_by and vector input",
fig = plot(df)
)
})


test_that("`plot.see_estimate_density()`, adding prior layers works", {
skip_if_not_installed("curl")
skip_if_offline()
Expand Down