Comments (12)
Glad you like colorblindr! What you are looking for can be done with the edit_colors
function. Ex.
p <- ggplot(iris, aes(Sepal.Width, fill=Species)) +
geom_density(alpha = 0.7)
p2 <- edit_colors(p, tritan, sev =0.8)
p3 <- edit_colors(p, deutan, sev = 1.0)
p4 <- edit_colors(p, protan, sev = 0.2)
p5 <- edit_colors(p, desaturate, amount = 0.1)
plot_grid(p, p2, p3, p4, p5)
This function is technically in the vignette, but @clauswilke we should probably add it to the main README.md. For when we submit it to CRAN, which is something that we should do.
from colorblindr.
I made a separate issue about CRAN release: #14
from colorblindr.
I suggest you leave the issue open, to remind us that we need to improve the documentation. Thanks!
from colorblindr.
Hi @clairemcwhite,
Yes, it's a super nifty package!
What I'm actually looking for is how to each individual plot without using plot_grid
. It seems that edit_colours
returns a gtable
, and then plot_grid
does something under the hood to recover this into a ggplot.
I have a bit of a workaround below to demonstrate what I mean:
library(colorblindr)
#> Loading required package: colorspace
#> Loading required package: ggplot2
library(tidyverse)
p <- ggplot(iris, aes(Sepal.Width, fill=Species)) +
geom_density(alpha = 0.7)
p2 <- edit_colors(p, tritan, sev =0.8)
# p2 is a gtable - I'm not sure how to convert this into a ggplot?
p2
#> TableGrob (12 x 11) "layout": 19 grobs
#> z cells name
#> 1 0 ( 1-12, 1-11) background
#> 2 5 ( 6- 6, 4- 4) spacer
#> 3 7 ( 7- 7, 4- 4) axis-l
#> 4 3 ( 8- 8, 4- 4) spacer
#> 5 6 ( 6- 6, 5- 5) axis-t
#> 6 1 ( 7- 7, 5- 5) panel
#> 7 9 ( 8- 8, 5- 5) axis-b
#> 8 4 ( 6- 6, 6- 6) spacer
#> 9 8 ( 7- 7, 6- 6) axis-r
#> 10 2 ( 8- 8, 6- 6) spacer
#> 11 10 ( 5- 5, 5- 5) xlab-t
#> 12 11 ( 9- 9, 5- 5) xlab-b
#> 13 12 ( 7- 7, 3- 3) ylab-l
#> 14 13 ( 7- 7, 7- 7) ylab-r
#> 15 14 ( 7- 7, 9- 9) guide-box
#> 16 15 ( 4- 4, 5- 5) subtitle
#> 17 16 ( 3- 3, 5- 5) title
#> 18 17 (10-10, 5- 5) caption
#> 19 18 ( 2- 2, 2- 2) tag
#> grob
#> 1 rect[plot.background..rect.86]
#> 2 zeroGrob[NULL]
#> 3 absoluteGrob[GRID.absoluteGrob.47]
#> 4 zeroGrob[NULL]
#> 5 zeroGrob[NULL]
#> 6 gTree[panel-1.gTree.33]
#> 7 absoluteGrob[GRID.absoluteGrob.40]
#> 8 zeroGrob[NULL]
#> 9 zeroGrob[NULL]
#> 10 zeroGrob[NULL]
#> 11 zeroGrob[NULL]
#> 12 titleGrob[axis.title.x.bottom..titleGrob.50]
#> 13 titleGrob[axis.title.y.left..titleGrob.53]
#> 14 zeroGrob[NULL]
#> 15 gtable[guide-box]
#> 16 zeroGrob[plot.subtitle..zeroGrob.82]
#> 17 zeroGrob[plot.title..zeroGrob.81]
#> 18 zeroGrob[plot.caption..zeroGrob.84]
#> 19 zeroGrob[plot.tag..zeroGrob.83]
class(p2)
#> [1] "gtable" "gTree" "grob" "gDesc"
# my workaround:
create_deutan <- function(c) colorspace::deutan(c, severity = 1)
plot_colorblind <- purrr::partial(.f = function(x) {
cowplot::plot_grid(
x,
scale = 0.9,
hjust = 0,
vjust = 1,
label_x = 0.01,
label_y = 0.99,
label_size = 12,
label_fontface = "bold"
)
}
)
gg_cb_deutan <- function(plot){
edited_colours <- colorblindr::edit_colors(plot, create_deutan)
plot_colorblind(edited_colours)
}
gg_cb_deutan(p)
Created on 2019-05-28 by the reprex package (v0.2.1)
For me, what I am looking for is if edit_colors
returned a ggplot - but perhaps returning a gtable is more flexible, in which case, maybe a autoplot method to work on it would work?
from colorblindr.
The function edit_colors()
simply returns a grob. You can draw it with grid functions as normal, or you can wrap it back into a ggplot with cowplot::ggdraw()
.
library(ggplot2)
library(colorspace)
library(colorblindr)
p <- ggplot(iris, aes(Sepal.Width, fill=Species)) +
geom_density(alpha = 0.7)
p2 <- edit_colors(p, deutan, severity = 0.8)
# plot using grid
library(grid)
grid.newpage()
grid.draw(p2)
# plot using cowplot
library(cowplot)
#>
#> *******************************************************
#> Note: cowplot does not change the default ggplot2 theme
#> anymore. To recover the previous behavior, execute:
#> theme_set(theme_cowplot())
#> *******************************************************
# ggdraw wraps the grob into a ggplot object
p3 <- ggdraw(p2)
class(p3)
#> [1] "gg" "ggplot"
p3
Created on 2019-05-27 by the reprex package (v0.2.1)
from colorblindr.
Or plain old plot
p <- ggplot(iris, aes(Sepal.Width, fill=Species)) +
geom_density(alpha = 0.7)
p2 <- edit_colors(p, tritan, sev = 1.0)
plot(p2)
Created on 2019-05-27 by the reprex package (v0.3.0)
from colorblindr.
Thanks for that!
It's great that there is an easy way to convert this into a plot.
I would find it very useful if there was a function like edit_colours
that also returned a plot. I still don't really know what a grob is (is it a data representation of a ggplot?) or what I am supposed to do with it, and I think that a convenience function that returns the plot and also edits the colours would be really useful.
from colorblindr.
I'm not convinced. edit_colors()
fundamentally works with grobs. It takes grobs as arguments and returns grobs. You can learn more about grobs here: https://bookdown.org/rdpeng/RProgDA/the-grid-package.html
The problem is that if we return a ggplot object, then people will expect that they can continue working with it like normal, e.g. adding themes etc., and that won't work anymore once it's been turned into a grob. In any case, we should explain more clearly what edit_colors()
returns and how one can work with it further.
from colorblindr.
The problem is that if we return a ggplot object, then people will expect that they can continue working with it like normal, e.g. adding themes etc.,
^^ This is what I was after, some kind of "magic" function that gives me my ggplot back, but with changed scales. Whenever I see a ggplot, my first feeling is that I should be able to do what I normally do to my ggplot objects - change axes, add themes, etc.
It sounds like in order for the edit_colours
function to work, it has to convert a ggplot into a grob, so that it can manipulate the underlying data of the plot, which can then no longer behave a regular ggplot - is that correct?
I think that your average user is not going to know what a grob is, and so I think it would be worthwhile explaining what this is, what edit_colors
returns, and how it works, and why the user should not expect it to behave like a regular ggplot. I don't think it should be expected for all users of ggplot to understand what a grob
is and what it means - or do you think that is something that more people should understand, when using ggplot?
from colorblindr.
It sounds like in order for the
edit_colours
function to work, it has to convert a ggplot into a grob, so that it can manipulate the underlying data of the plot, which can then no longer behave a regular ggplot - is that correct?
Yes, this is correct. It works exactly like the various other cowplot functions (plot_grid()
, ggdraw()
, draw_plot()
, etc.), where I frequently encounter similar misunderstandings. I think we should refer people to the cowplot documentation (which I'm currently updating) rather than explaining the same general issue again. Part of the purpose of cowplot is to enable regular users to do things that need grob-level access but without knowing about grobs.
from colorblindr.
OK I think I understand better now.
grobs are an interesting problem to abstract away - I didn't really realise that is what was actually happening, but now that I take the time to think about it, it does make sense.
I think it's one of these things that can be a really tricky experiences for the user, in that they see a ggplot, so it kind of has this, "looks like a duck, quacks like a duck, therefore a duck" thing going on.
If you'd be interested, I'd be happy to review some of the documentation you write for cowplot re grobs?
In any case, I'm happy to close this issue - but I think that the pattern:
library(ggplot2)
library(colorspace)
library(colorblindr)
p <- ggplot(iris, aes(Sepal.Width, fill=Species)) +
geom_density(alpha = 0.7)
p2 <- edit_colors(p, deutan, severity = 0.8)
# plot using grid
library(grid)
grid.newpage()
grid.draw(p2)
# plot using cowplot
library(cowplot)
#>
#> *******************************************************
#> Note: cowplot does not change the default ggplot2 theme
#> anymore. To recover the previous behavior, execute:
#> theme_set(theme_cowplot())
#> *******************************************************
# ggdraw wraps the grob into a ggplot object
p3 <- ggdraw(p2)
class(p3)
#> [1] "gg" "ggplot"
p3
plot(p3)
Would be worthwhile documenting in coloblindr.
Thanks again for the discussion and for taking the time to explain things, it is very much appreciated!
from colorblindr.
OK no worries.
from colorblindr.
Related Issues (17)
- add vignette on adding images to plots? HOT 3
- Error in installing - cannot find desaturate from colorspace HOT 10
- Error: Does not appear to be an R package (no DESCRIPTION) HOT 1
- Release colorblindr on CRAN HOT 2
- geom_label() sneaks past cvd_grid() HOT 4
- Just checking - no continuous functionality? HOT 2
- Failed to install 'colorblindr' from GitHub HOT 1
- Feature request: a function that scores a specific color wrt "colorblindness perceivability"
- cvd_grid() error HOT 10
- install via remotes::install_github("clauswilke/colorblindr") throws error
- availability for 'Beagle Scouts' (4.3.1)
- Trouble installing colorblindr HOT 7
- the repo lacks a description HOT 1
- not able to install colorblindr HOT 1
- Feature request: Separate palettes from colorblind functionality HOT 1
- Use `last_plot()` as default `plot` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from colorblindr.