wilkelab / ggridges

Ridgeline plots in ggplot2
https://wilkelab.org/ggridges
GNU General Public License v2.0
411 stars 31 forks source link

feature_request: vertical segment lines #40

Open kissmyjazz opened 5 years ago

kissmyjazz commented 5 years ago

Hello, Thank you for the wonderful package! I saw that it is possible to plot quantile segments on ridge densities, but I would like to plot vertical segments where x coordinates come from my data frame and y coordinates are matched to the height of ridge density in a grouped data at a particular x coordinate. I could not figure out how to map ggplot's geom_segment into the coordinate space of ridge densities.

clauswilke commented 5 years ago

I would need a specific example to understand what exactly you are requesting.

For any examples you prepare, please use the reprex package and follow their guidelines on minimal examples.

kissmyjazz commented 5 years ago

I have a count variable binned into time periods. I represent counts with the geom_density_ridges() with stat "identity". I also have a second variable that I would like to represent which specifies the onset of an event for each observation. Now these data is represented below ridges by green bars by using geom_segment(). I would like to map those segments over the ridges because each ridge is associated with the specific onset. Sorry, I could not find the built in dataset that represents similar data structure.

image

kissmyjazz commented 5 years ago
library(ggplot2)
library(ggridges)

d <- data.frame(x = rep(1:5, 3), y = c(rep(0, 5), rep(1, 5), rep(2, 5)),
                height = c(0, 1, 3, 4, 0, 1, 2, 3, 5, 4, 0, 5, 4, 4, 1),
                path = rep(1:3, 5))
ggplot(d, aes(x, y, height = height, group = y)) + 
  geom_density_ridges(stat = "identity", scale = 1) + 
  geom_segment(data = d, aes(x = path, xend = path, y = height, yend = height-0.5),
               color = "green3")

Created on 2019-07-09 by the reprex package (v0.3.0)

kissmyjazz commented 5 years ago

How can I map the height of green bars to match the height of the ridges?

clauswilke commented 5 years ago

By inspecting the y axis scale, you should be able to deduce how you can scale the green bars so they fit. Overall, this doesn't look like a feature that makes sense to add to ggridges.

kissmyjazz commented 5 years ago

ggridges already scales quantile bars to a proper height. How can I extract scaled y height information from the ggridges object?