Skip to content

Instantly share code, notes, and snippets.

@chichacha
Created July 28, 2019 01:07
Show Gist options
  • Save chichacha/2a1aff0acd558cf8860dd95475a084bb to your computer and use it in GitHub Desktop.
Save chichacha/2a1aff0acd558cf8860dd95475a084bb to your computer and use it in GitHub Desktop.
Uploading RMD file using gistr package

title: "Testing gistr package" author: "Chisato" date: "27/07/2019" output: html_document: highlight: kate theme: spacelab toc: yes

Creating Gradient Colours from Colour Palettes

## Get Your Favourite Colour - I am creating named vector
colpal <- c("red"="#fe4a49", "blue"="#2ab7ca","yellow"= "#fed766","grey"= "#e6e6ea", "white"="#f4f4f8")
attr(colpal,"names")
## [1] "red"    "blue"   "yellow" "grey"   "white"
metro_pal <- c("#d11141", "#00b159", "#00aedb", "#f37735", "#ffc425")

## Quick and Dirty way to show colours in a plot (along with hexcode)
show_col(colpal, labels=T, borders=NA, cex_label=1.5)

plot of chunk colors

show_col(metro_pal, labels=T, border=NA)

plot of chunk colors

## I think I'm in love with this function colorRampPalette
### Let's say I want to get 16 colour palettes from 5 colours
# bias must be positive number, higher values give more widely spaced colours at the high end.
# space - you can pick rgb or Lab
# interpolate - "linear" or "spline

show_col(colorRampPalette(colpal, space="Lab")(16), border=NA)

plot of chunk colors

show_col(colorRampPalette(colpal, space="rgb")(16), border=NA)

plot of chunk colors

show_col(colorRampPalette(colpal, interpolate="spline")(16), border=NA)

plot of chunk colors

show_col(colorRampPalette(colpal, interpolate="linear")(16), border=NA)

plot of chunk colors

colpal_16 <- colorRampPalette(colpal, interpolate="spline")(16)

## You can create a raster image by below
bg <-matrix(colorRampPalette(colpal,interpolate="spline")(50*50), nrow=1)

a <-tibble(x=seq(1,5, length.out=5), y=seq(1,20, length.out=5)) %>% 
  ggplot() +
  annotation_raster(raster=bg, xmin=1,xmax=5,ymin=1,ymax=20) +
  geom_blank(aes(x=x,y=y))+
  theme_void() +
  annotate(geom="text", x=3, y=10.5, label="No Interpolation", 
           family="Avenir",
           size=20, color="#ffffffde")

b <-tibble(x=seq(1,5, length.out=5), y=seq(1,20, length.out=5)) %>% 
  ggplot() +
  annotation_raster(raster=bg, xmin=1,xmax=5,ymin=1,ymax=20, interpolate=T) +
  geom_blank(aes(x=x,y=y))+
  theme_void() +
  annotate(geom="text", x=3, y=10.5, label="Interpolated", 
           family="Avenir",
           size=20, color="#ffffffde")

a + b + plot_layout(ncol=1)

plot of chunk colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment