clauswilke / colorblindr

An R package to simulate colorblindness on R figures.
MIT License
306 stars 28 forks source link

geom_label() sneaks past cvd_grid() #15

Open k-hench opened 4 years ago

k-hench commented 4 years ago

Hello,

I just wanted to give a short heads up about a minor issue I stumbled upon:

when playing around with this great package I just realized some unexpected behaviour with when using geom_label() in combination cvd_grid() - somehow the color of this particular geom stays untouched:

library(tidyverse)
library(colorblindr)
#> Loading required package: colorspace

p <- tibble(label = 'test') %>%
  ggplot()+
  geom_segment(aes(x = -1, xend = 1,y = 0, yend = 0, color = label))+
  geom_label(aes(x=0,y=0,label = label, color = label))+
  theme_void()

cvd_grid(p)

Created on 2019-08-27 by the reprex package (v0.3.0)

cheers, Kosmas

clauswilke commented 4 years ago

Thanks for pointing this out. Unfortunately, I have realized that these kinds of issues are unavoidable. There'll always be some grob that colorblindr can't properly handle. (I'm working on some right now.) Moving forward, it's probably best to just generate an image and then convert that. The colorspace package has an app that simulates color vision deficiency on images. This may be useful for you.

k-hench commented 4 years ago

Thanks for the quick note. Following your advice, I reached this solution (which should be independent of special grobs):

library(tidyverse)
library(colorblindr)
#> Loading required package: colorspace
library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************
library(grImport2)
library(grConvert)

p <- tibble(label = 'test') %>%
  ggplot()+
  geom_segment(aes(x = -1, xend = 1,y = 0, yend = 0, color = label))+
  geom_label(aes(x=0,y=0,label = label, color = label))+
  theme_void()

ggsave(plot = p,filename = '~/Desktop/p.svg',width = 2,height = 1)
grConvert::convertPicture('~/Desktop/p.svg','~/Desktop/p.c.svg')
#> '/home/khench/Schreibtisch/p.svg' (SVG) was converted to '/home/khench/Schreibtisch/p.c.svg' (Cairo SVG)

p_detour <- ggdraw(grImport2::readPicture('~/Desktop/p.c.svg') %>% 
                     grImport2::pictureGrob(.))

cvd_grid(p_detour)

Created on 2019-08-27 by the reprex package (v0.3.0)

clauswilke commented 4 years ago

Please don't close the issue. It is real, and I may look into fixing it at some point.

k-hench commented 4 years ago

ups, sorry (I'm still github newbie...)