Skip to content

Add support for ggridges - #2314

Merged
cpsievert merged 5 commits into
plotly:masterfrom
AdroMine:ggridges_support
May 8, 2024
Merged

Add support for ggridges#2314
cpsievert merged 5 commits into
plotly:masterfrom
AdroMine:ggridges_support

Conversation

@AdroMine

Copy link
Copy Markdown
Contributor

Closes#1912

Add support for ggrides package

I took inspiration from the #1087 and have a version that supports most of the ggridges functionality. The test file has all the examples from ggridges introduction vignette

What's working:

  • geom_ridgeline
  • geom_density_ridges (including custom densities (stat = identity), bin shape, custom heights)
  • geom_density_ridges2
  • geom_ridgeline_gradient
  • stat_density_ridges
  • stat_denstity_ridges with gradients
  • ridges with jittered points / raincloud effect, points variations

What's not working:

  • geom_ridgeline_gradient with continuous colours
  • geom_vridgeline

ggridge functions that already working without new code:

  • themes (axis title placement is off)
  • scale_fill_cyclical (custom label names don't work)

Examples:

(p<- ggplot(iris, aes(x=Sepal.Length, y=Species, fill=factor(stat(quantile)))) +
stat_density_ridges(
geom="density_ridges_gradient",
calc_ecdf=TRUE,
quantiles= c(0.025, 0.975)
) +
scale_fill_manual(
name="Probability", values= c("#FF0000A0", "#A0A0A0A0", "#0000FFA0"),
labels= c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]")
))

image

ggplotly(p)

image

(p2<- ggplot(iris, aes(x=Sepal.Length, y=Species)) +ggridges::geom_density_ridges(fill='midnightblue', alpha=0.6))

image

ggplotly(p2)

image

@cpsievert

Copy link
Copy Markdown
Collaborator

Wow, incredible work, thank you! I'm going to add some nit-picky comments, but overall this is looking great

Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R Outdated
Comment threadtests/testthat/test-ggridges.R Outdated
@cpsievert

cpsievert commented Nov 10, 2023

Copy link
Copy Markdown
Collaborator

I wouldn't consider this a blocking issue, and I'm not sure if this is sensible to address here, but just throwing it out there that it'd be awesome if this worked better (more specifically, it seems highlight()'s opacityDim isn't having an effect on the non-highlighted ridgelines?)

p<- ggplot(iris|> highlight_key(~Species), aes(x=Sepal.Length, y=Species)) + geom_density_ridges(rel_min_height=0.01)
ggplotly(p) |> highlight("plotly_hover", opacityDim=0.2)

@AdroMine

Copy link
Copy Markdown
ContributorAuthor

I wouldn't consider this a blocking issue, and I'm not sure if this is sensible to address here, but just throwing it out there that it'd be awesome if this worked better (more specifically, it seems highlight()'s opacityDim isn't having an effect on the non-highlighted ridgelines?)

p<- ggplot(iris|> highlight_key(~Species), aes(x=Sepal.Length, y=Species)) + geom_density_ridges(rel_min_height=0.01)
ggplotly(p) |> highlight("plotly_hover", opacityDim=0.2)

Didn't know about highlight. Any idea what could be affecting it? Or where I could look to debug?

Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R Outdated
Comment threadR/ggridges.R
Comment on lines +253 to +258
text = uniq(data[["hovertext"]]),
key = data[["key"]],
customdata = data[["customdata"]],
frame = data[["frame"]],

@cpsievertcpsievertNov 10, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these need be doubled, like you did for x/y (this might be leading to #2314 (comment))

I'd consider doing something like (using with() here can be kinda dangerous since it'll error if any of the columns are missing

positions<- rbind(data, rev(data))
positions$y<- c(data$ymax, rev(data$ymin))

@AdroMineAdroMineNov 11, 2023

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the issue! Within the new to_basics, I was using to_basic.GeomDensity and to_basic.GeomLine which removed the crosstalk set. Attaching them back again similar to how it's done in layers2traces solves the highlight not working.

Before this, the to_basic.GeomLine would preserve the set attribute, but to_basic.GeomDensity would not, so we were getting highlight working only the line part and not density/area part.

I have tested the highlight functionality with different types of ridge plots, with different fills, points, lines within them, and they are all working as expected.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpsievert are there any pending action items on this, or is this good to go now?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for letting this slip @AdroMine, looking good, would you mind just adding a NEWS.md item?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpsievert , added a line about adding support for ggridges package.

@ashirwad

This comment was marked as off-topic.

@cpsievert

Copy link
Copy Markdown
Collaborator

Awesome work, thank you!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ggridges not implemented for ggplotly

3 participants

@AdroMine@cpsievert@ashirwad