Skip to content

Density not visible when y only has one level #100

@see24

Description

@see24

When y only has one value I am getting a flat line for the density plot. I imagine something is going wrong with the scaling. This is coming up inside a function where I want the plot to look decent regardless of how many ridges will be plotted.

library(ggplot2)
library(ggridges)

dat <- data.frame(id = "species1", value = rnorm(1000, mean = 2500, sd = 1000))

ggplot(dat, aes(x = value, y = id))+
  geom_density_ridges()
#> Picking joint bandwidth of 221

# Doesn't seem to be caused by stat_density_ridges
ggplot(dat, aes(x = value, y = id))+
  geom_density_ridges(aes(height = after_stat(density)), stat = "density")

ggplot(dat, aes(x = value))+
  geom_density()

ggplot(dat, aes(x = value, y = 1))+
  geom_density_ridges()
#> Picking joint bandwidth of 221

# Works fine if there are two ridges
dat2 <- data.frame(id = rep(c("species1", "species2"), 1000) |> sort(),
                   value = c(rnorm(1000, mean = 2500, sd = 1000),
                             rnorm(1000, mean = 1500, sd = 2000)))

ggplot(dat2, aes(x = value, y = id))+
  geom_density_ridges()
#> Picking joint bandwidth of 328

Created on 2025-04-03 with reprex v2.1.1

 packageVersion("ggridges")
[1] ‘0.5.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions