Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ridgeline plot from ggridges. [WIP] #258

Closed
wants to merge 2 commits into from
Closed

Adding ridgeline plot from ggridges. [WIP] #258

wants to merge 2 commits into from

Conversation

jenzopr
Copy link

@jenzopr jenzopr commented Feb 9, 2018

Hi Barret,

I recently started to include ridgeline plots in my ggpairs figures that I create for some projects. I thought it would be a nice idea to make those plots available for all GGally users.

This PR adds a wrapper for ggridges::geom_density_ridges as ggally_density_ridges.
It does not yet do much, except making sure the plot is horizontal and the color mapping is mapped to fill instead.

Since there might be some pitfalls when including into GGally, I would like to hear your comments and later extend this PR.

Looking forward to your reply.
Best,
Jens

@codecov-io
Copy link

codecov-io commented Feb 9, 2018

Codecov Report

Merging #258 into master will decrease coverage by 0.18%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #258      +/-   ##
=========================================
- Coverage   97.78%   97.6%   -0.19%     
=========================================
  Files          25      25              
  Lines        4246    4254       +8     
=========================================
  Hits         4152    4152              
- Misses         94     102       +8
Impacted Files Coverage Δ
R/gg-plots.R 98.08% <0%> (-1.27%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0dc3283...c4e431c. Read the comment docs.

@schloerke
Copy link
Member

Hi Jens,

I like the idea, but I'm cautious about the movement of label position. Because of this I don't know if I can include it. I'd like to be a stickler to that rule.

However, I would like to make it easier to extend the functionality, so ggridge can use the simple character syntax. This way, ggridge can include ggally_density_ridges in their package, and it should work with

ggpairs(iris, 3:5, lower = list(combo = "density_ridges"))

screen shot 2018-02-09 at 10 16 26 am

Needed fixes:

  • export mapping_swap_x_y
  • export mapping_color_to_fill
  • export is_horizontal
  • fix where functions are retrieved

I'd also like to include a "Known Extensions" or something somewhere on GitHub. Idk how it'd work as a pkg vignette, as I don't want to make circular dependancies. Even a list that points to GitHub/cran docs would be a great start.

Thoughts?

  • Barret





Works with user session, but not with reprex::reprex (which is 'cleaner'). Need to not look in just GGally package, but just for 'ggally_*'.

library(GGally)

ggally_density_ridges <- function(data, mapping, ...) {
  require(ggridges)
  horizontal <- GGally:::is_horizontal(data, mapping)
  
  if (!horizontal) {
    mapping <- GGally:::mapping_swap_x_y(mapping)
  }
  
  mapping <- GGally:::mapping_color_to_fill(mapping)
  
  ggplot2::ggplot(data) + ggridges::geom_density_ridges2(mapping, ...)
}

## really neat!  Each label is at the correct position, but overlap is
## achieved
ggpairs(iris, 3:5, lower = list(combo = "density_ridges"))
#> Error in value[[3L]](cond): The following ggpairs plot functions are readily available: 
#>  continuous: c('points', 'smooth', 'smooth_loess', 'density', 'cor', 'blank')
#>  combo: c('box', 'box_no_facet', 'dot', 'dot_no_facet', 'facethist', 'facetdensity', 'denstrip', 'blank')
#>  discrete: c('ratio', 'facetbar', 'blank')
#>  na: c('na', 'blank')
#> 
#>  diag continuous: c('densityDiag', 'barDiag', 'blankDiag')
#>  diag discrete: c('barDiag', 'blankDiag')
#>  diag na: c('naDiag', 'blankDiag')
#> 
#> You may also provide your own function that follows the api of function(data, mapping, ...){ . . . }
#> and returns a ggplot2 plot object
#>  Ex:
#>  my_fn <- function(data, mapping, ...){
#>    p <- ggplot(data = data, mapping = mapping) + 
#>      geom_point(...)
#>    p
#>  }
#>  ggpairs(data, lower = list(continuous = my_fn))
#> 
#> Function provided: density_ridges

ir <- iris
ir$Species <- factor(as.character(iris$Species), levels = c("versicolor", "virginica", 
  "setosa"))

## labels on the left side are squished, as 'setosa' requires more space.
ggpairs(ir, 3:5, lower = list(combo = "density_ridges"))
#> Error in value[[3L]](cond): The following ggpairs plot functions are readily available: 
#>  continuous: c('points', 'smooth', 'smooth_loess', 'density', 'cor', 'blank')
#>  combo: c('box', 'box_no_facet', 'dot', 'dot_no_facet', 'facethist', 'facetdensity', 'denstrip', 'blank')
#>  discrete: c('ratio', 'facetbar', 'blank')
#>  na: c('na', 'blank')
#> 
#>  diag continuous: c('densityDiag', 'barDiag', 'blankDiag')
#>  diag discrete: c('barDiag', 'blankDiag')
#>  diag na: c('naDiag', 'blankDiag')
#> 
#> You may also provide your own function that follows the api of function(data, mapping, ...){ . . . }
#> and returns a ggplot2 plot object
#>  Ex:
#>  my_fn <- function(data, mapping, ...){
#>    p <- ggplot(data = data, mapping = mapping) + 
#>      geom_point(...)
#>    p
#>  }
#>  ggpairs(data, lower = list(continuous = my_fn))
#> 
#> Function provided: density_ridges

@jenzopr
Copy link
Author

jenzopr commented Feb 12, 2018

Hi Barret,

nice feature, I didn't know that it would look in the global env for a function prefixed by ggally_.
For the labels, I could not reproduce the overlap with the code that you gave at the end of the comment.

Best,
Jens

schloerke added a commit that referenced this pull request Feb 10, 2020
@schloerke schloerke mentioned this pull request Feb 10, 2020
1 task
@schloerke
Copy link
Member

Closing in favor of #309

@schloerke schloerke closed this Feb 10, 2020
Sign up for free to 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.

3 participants