Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
36 changes: 31 additions & 5 deletions R/plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ 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
if (length(dataplot$Parameter) == 0 ||
all(is.na(dataplot$Parameter)) ||
all(dataplot$Parameter == "") ||
length(unique(dataplot$Parameter[!is.na(dataplot$Parameter)])) == 0) {
dataplot$Parameter <- rep("Distribution", nrow(dataplot))
}
}

# add component and effects columns
if (!is.null(data)) {
Expand All @@ -23,11 +34,20 @@ 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
if ("Parameter" %in% names(dataplot) &&
length(dataplot$Parameter) > 0 &&
length(dataplot$Parameter) == nrow(dataplot) &&
!all(is.na(dataplot$Parameter))) {
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 +270,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
12 changes: 12 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,18 @@ 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")

# 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")
})


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