Git Product home page Git Product logo

ggchromatic's People

Contributors

teunbrand avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

gejielin

ggchromatic's Issues

Add example for custom color space

I think custom color scales are important for multidimensional color scales, because different colorspaces imply different dependencies of the differente channels amongst each other, and create different "perceptual correlations" in the viewer. Therefore being able to create a custom made colorspace for a particular application is useful.
When trying to do so with the package I first had the impression that it is very difficult to do due to its (at first sight) tight integration with the farver package, which quickly leads one to calls of c/c++ functions.
Then later I found out that it's actually quite simple to implement a custom colorspace. It would be great it the welcome page listed one such example to show how simple it can be and to give users a headstart.
Here's a simple example, probably not in full production quality. It could be used as a basis to make a good example for the starting page.

library(tidyverse)
library(ggchromatic)

# define a custom palette function
ab_spec=
    function(observationEffort=double(),nDetections=double()){
        ggchromatic:::new_colour_spec(
            observationEffort=observationEffort
            ,nDetections=nDetections
            ,class="ab_spec"
            )
    }
ab_palette=
    function (x, min = 0, max = 1) 
    {
        x=as.matrix(x)
        # print(x)
        out=character(nrow(x))
        inputsWithNA=is.na(x[,"observationEffort"])|is.na(x[,"nDetections"]) # only run rgb() on inputs without NA, as it doesnt have NA-handling implemented. return NA for all inputs that contain NA in any channel.
        out[inputsWithNA]=NA
        out[!inputsWithNA]=rgb(x[!inputsWithNA,"observationEffort"],x[!inputsWithNA,"nDetections"],1-x[!inputsWithNA,"observationEffort"])
        out
    }


# apply the custom palette
n=30
tibble(observationEffort=(1:n)/n,nDetections=runif(n)) %>% 
    ggplot()+
    geom_point(
        aes(
            observationEffort
            ,nDetections
            ,colour=ab_spec(observationEffort,nDetections)
        )
    )+
    chromatic_scale(
        aesthetics="colour"
        ,name=c("observation effort","number of\ndetections")
        ,scale_name="ab_scale"
        ,palette=ab_palette
        ,prototype=ab_spec
        ,limits=
            list(
                observationEffort=c(0,1)
                ,nDetections=c(0,1)
                )
        ,channel_limits=ab_spec(0:1,0:1)
        ,expand=0
    )

Move over discussion

This issue is for continuing the thread over at teunbrand/ggh4x#29.

  • I've not carried over the colour cube yet
  • Some channels can now be discrete, but haven't tested exhaustively yet
  • There is now a HSV scale

Error generating cube

I am trying to do a umap graph with 3 genes and I can get the color to work, but then when it has to create the legend it fails with the error:
Error in if (0 <= angle & angle < 90) { : the condition has length >

trace back info:

  1. rotate_just(angle, hjust, vjust)
  2. title_spec(label, x = x, y = y, hjust = hjust, vjust = vjust,
    angle = angle, gp = gp, debug = debug, check.overlap = check.overlap)
  3. titleGrob(label, x, y, hjust = hj, vjust = vj, angle = angle,
    gp = modify_list(element_gp, gp), margin = margin, margin_x = margin_x,
    margin_y = margin_y, debug = element$debug, ...)
  4. element_grob.element_text(title.theme, label = label, angle = ang,
    x = unit(pos[, 1], "cm"), y = unit(pos[, 2], "cm"), hjust = 0.5,
    vjust = 0, )
  5. element_grob(title.theme, label = label, angle = ang, x = unit(pos[,
    1], "cm"), y = unit(pos[, 2], "cm"), hjust = 0.5, vjust = 0,
    )
  6. withCallingHandlers(expr, warning = function(w) if (inherits(w,
    classes)) tryInvokeRestart("muffleWarning"))
  7. suppressWarnings(element_grob(title.theme, label = label, angle = ang,
    x = unit(pos[, 1], "cm"), y = unit(pos[, 2], "cm"), hjust = 0.5,
    vjust = 0, ))
  8. build_cube_titles(guide, theme, axes$params)
  9. guide_gengrob.colourcube(X[[i]], ...)
  10. FUN(X[[i]], ...)
  11. lapply(gdefs, guide_gengrob, theme)
  12. guides_gengrob(gdefs, theme)
  13. build_guides(plot$scales, plot$layers, plot$mapping, position,
    theme, plot$guides, plot$labels)
  14. ggplot_gtable.ggplot_built(data)
  15. ggplot_gtable(data)
  16. print.ggplot(x)
  17. (function (x, ...)
    UseMethod("print"))(x)

If i suppress the legend with theme(legend.position='none') I can get the graph to work. Similarly if I only use two genes I can see the graph and the legend.
I made a dataframe with the umap coordinates and the SCT normalized expression for 3 genes. Tried to look at your code to figure out what was wrong with my input. I am running this on a docker image with RStudio with ggplot2 3.4.3.

Here is my code
tes<-subset(immune_combined2, features= c('Zbtb16','Gata3','Rorc'))
FD<-cbind(immune_combined2@reductions$[email protected],t(tes@assays$SCT$data))
FD<-data.frame(FD)
g1<-ggplot(FD, aes(x = umap_1, y = umap_2))+geom_point(aes(colour = rgb_spec(Zbtb16, Gata3, Rorc)))+theme_classic()

add support for differenc rescalers for the channels

Since the different channels might serve to visualize very different things, they sometimes need different rescalers.
As far as I can see this currently is not possible, since:

  1. the rescaler is called on the individual channels data rather then the color_spec vector.
  2. nor is it not possible to specify different rescalers for the different channels.
  3. nor is the rescaler given any info about which channel it is currently being invoked on.

I currently have a project where I want to make use of this feature, because 1 channel contains outliers and must thus be scaled based on quantiles rather then minimum and maximum to give a useful visualisation, whereas the other channel must be guaranteed to include all extreme values and is therefore scaled based on min/max.
As a sidenote: ggplot does not naturally support scaling based on quantiles, as the only info about the data which is passed to the rescaler is the min and max (e.g. when the rescaler is invoked in the legend construction, where no actual data is passed). I'm circumventing this by scoping the full dataset into the rescaler in a separate channel.

Easier channel limits

To control the output of the individual channels.
Now, you'd have to specify for example:

scale_colour_rgb(
  channel_limits = rgb_spec(c(0, 1), c(0.25, 0.75), c(0, 1))
)

It would be nicer if you could just do:

scale_colour_rgb(
  channel_limits = list(g = c(0.25, 0.75))
)

Relative colourspace

The idea is that you can graft a regular palette onto a colourspace palette and adjust chroma/saturation and lightness.
For example, we take the viridis palette as channel 1, put chroma/saturation as channel 2 and lightness as channel 3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.