Convenient R code snippets

Some links to convenient color palettes etc

The pals package offers nice interactive testing of the palettes, also includes many palettes:

https://cran.r-project.org/web/packages/pals/vignettes/pals_examples.html

Some other websites:

Some miscelaneous example maps:

All links/pics above come from a LinkedIn post by dr. Richard Stevens.

Font size in plots

To get functions like geom_text to print a certain font size, use the hidden conversion parameter “.pt”:
#(..) +
geom_text(aes(label=round(corr,2)), color='#666666', size=8/.pt)

Geom_text_repel

For it to always draw all labels and lines, use:
max.overlaps = Inf
min.segment.length = 0

Changing font in ggplot

Changing fonts is a bit of a pain, it depends on the graphics
device that’s being used.

I first thought it would be required to change fonts in either
the pdf() device, which would mean changing things in postscriptFonts(),
or the quartz device, which would mean changes in quartzFonts(); additionally there’s also the X11 device.

Anyways, turns out easiest is to just use the “cairo_pdf”, which just accepts “Arial” as a font. See example below:

library(ggplot2)

p=ggplot(data.frame(x=runif(10), y=runif(10)))+
geom_point(aes(x=x, y=y))+

ggtitle('Blablabla')+xlab('test239492048 adskhaskdhf')+ylab('sdkjfhak ekjah kjh')+
theme(text=element_text(family='Arial'))

p

ggsave(filename = '~/Desktop/test.pdf', width=10, height=10, units='cm', plot = p, device = cairo_pdf)

This worked on my Macbook where the font was installed. But at the HPC I work with, I also had to install the fonts, which I did using:
fc-list | grep Arial # check whether it's there (wasn't)
fc-list # to determine where other fonts where
# Uploaded the Arial fonts (used "Font Book" on Mac to locate locally) to dir
fc-cache -f -v # To load the fonts