Skip to content

position_jitterdodge() with jitter.height > 0 can incorrectly produce negative x-values on plot #6535

@heinonmatti

Description

@heinonmatti

Description
When using position_jitterdodge() with jitter.height > 0 on a plot with numeric x-axis and categorical y-axis, points are incorrectly positioned at negative x-values even when all data points are positive.

Expected Behavior
Vertical jitter (jitter.height) should only affect the y-position of points and should not influence x-coordinates. All points with positive x-values in the data should remain at positive x-positions in the plot.

Actual Behavior
Setting jitter.height > 0 causes some points to be plotted at negative x-coordinates, even though all x-values in the source data are positive.

library(ggplot2)
library(dplyr)

# Create test data
set.seed(42)
test_data <- expand.grid(
  feature = factor(paste0("feature_", 1:10)),
  group = factor(c("Group1", "Group2", "Group3", "Group4", "Group5")),
  rep = 1:20
) %>%
  mutate(
    # Create positive values clustered near zero (like SHAP variances)
    value = rexp(n(), rate = 10)
  )


# Plot 1: Without jitter.height (correct behavior)
p1 <- ggplot(test_data, aes(x = value, y = feature, alpha = group)) +
  geom_point(
    position = position_jitterdodge(
      jitter.width = 0,
      jitter.height = 0,  # No vertical jitter
      dodge.width = 0.7
    ),
    color = "grey40",
    size = 1
  ) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", size = 1) +
  scale_x_continuous(limits = c(-0.05, 0.5)) +
  theme_minimal() +
  labs(title = "jitter.height = 0 (All points correctly positive)")

# Plot 2: With jitter.height (bug? Shows negative values)
p2 <- ggplot(test_data, aes(x = value, y = feature, alpha = group)) +
  geom_point(
    position = position_jitterdodge(
      jitter.width = 0,
      jitter.height = 0.05,  # Vertical jitter causes the bug
      dodge.width = 0.7
    ),
    color = "grey40",
    size = 1
  ) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed", size = 1) +
  scale_x_continuous(limits = c(-0.05, 0.5)) +
  theme_minimal() +
  labs(title = "jitter.height = 0.05 (Points incorrectly appear negative)")

# Show both plots
library(patchwork)
combined <- p1 / p2
print(combined)

Image

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions