Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggrepel label with transparent background but visible font

Tags:

r

ggplot2

ggrepel

Is there somehow a trick to get the font within 'geom_label_repel' alpha=1 but the background maybe alpha=.2?

My problem is, that I have sometimes very dense plots. If I use just text, the text is not readable anymore. If I use label without transparency, the label is perfectly readable but I can not see behind the label. If I choose transparency for the label, then again, the font is no longer readable since it is also transparent and there is not enough contrast against the background.

What I would really love is a white shadow around the font :-)

Here is a minimal example do demonstrate the problem.

library(ggplot2)
library(ggrepel)
library(stringi)

set.seed(1)
df <- data.frame(x=rnorm(10000),
                 y=rnorm(10000),
                 label=NA)
df$label[1:26] <- stringi::stri_rand_strings(26,8)

ggplot(df, aes(x, y)) +
  geom_point(alpha=.3) +
  geom_label_repel(aes(label=label),
                   label.size = NA, 
                   alpha = 0.6, 
                   label.padding=.1, 
                   na.rm=TRUE) +
  theme_bw()

enter image description here

like image 533
drmariod Avatar asked Feb 01 '18 08:02

drmariod


3 Answers

Plot two labels, the second with no fill at all. Set the seed to make sure they perfectly overlap. (Using geom_text_repel doesn't seem to work as the repelling works slightly different.)

ggplot(df, aes(x, y)) +
  geom_point(alpha=.3) +
  geom_label_repel(aes(label=label),
                   label.size = NA, 
                   alpha = 0.6, 
                   label.padding=.1, 
                   na.rm=TRUE,
                   seed = 1234) +
  geom_label_repel(aes(label=label),
                   label.size = NA, 
                   alpha = 1, 
                   label.padding=.1, 
                   na.rm=TRUE,
                   fill = NA,
                   seed = 1234) +
  theme_bw()

enter image description here

like image 143
Axeman Avatar answered Nov 17 '22 17:11

Axeman


ggplot(df, aes(x, y)) +
  geom_point(alpha=.3) +
  geom_label_repel(aes(label=label),
                       label.size = NA,  
                       label.padding=.1, 
                       na.rm=TRUE,
                       fill = alpha(c("white"),0.5))

This worked for me. You can set alpha with a color. Since the fill setting is only the background the text is unaffected. The advantage of this to overlaying the text is you can still use the "repel" to keep text from overlapping and not have to worry about getting two layers lining up properly

like image 14
spiketheaardvark Avatar answered Nov 17 '22 15:11

spiketheaardvark


Maybe something like the following one:

library(ggplot2)
library(ggrepel)
library(stringi)

set.seed(1)
df <- data.frame(x=rnorm(10000),
                 y=rnorm(10000),
                 label=NA)
df$label[1:26] <- stringi::stri_rand_strings(26,8)

ggplot(df, aes(x, y)) +
    geom_point(alpha=.3) +
    geom_label_repel(aes(label=label),
                     label.size = NA, 
                     alpha = 0.75, 
                     fontface = 'bold', color = 'black',
                     box.padding = 0.80, point.padding = 0.5,
                     na.rm=TRUE) +
    theme_bw()

which gives: enter image description here

like image 5
Scipione Sarlo Avatar answered Nov 17 '22 16:11

Scipione Sarlo