├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── R ├── create_ggtheme.R ├── display_colours.R ├── extract_colours.R ├── interpolate_colours.R ├── mpd_select_colours.R ├── palette_reduce.R ├── plot_radar.R └── simulate_colours.R ├── README.md ├── man ├── create_ggtheme.Rd ├── display_colours.Rd ├── extract_colours.Rd ├── interpolate_colours.Rd ├── mpd_select_colours.Rd ├── palette_reduce.Rd ├── plot_radar.Rd └── simulate_colours.Rd └── rPlotter.Rproj /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | 4 | # Example code in package build process 5 | *-Ex.R 6 | 7 | # R data files from past sessions 8 | .Rdata 9 | .Rproj.user 10 | 11 | # Other Stuff 12 | README.html 13 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: rPlotter 2 | Type: Package 3 | Title: rPlotter: Easy Plotting in R 4 | Version: 0.0.8 5 | Date: 2014-07-223 6 | Authors@R: c( 7 | person("Russell", "Dinnage", role = "ctb"), 8 | person("Jo-fai", "Chow", email = "jofai.chow@gmail.com", 9 | role = c("aut", "cre") 10 | )) 11 | Maintainer: Jo-fai Chow 12 | Description: A collection of wrapper functions for graphics. The objective of 13 | this package is to make plotting easier for R beginners. 14 | License: MIT 15 | Depends: 16 | R (>= 3.0.0), 17 | rblocks 18 | Imports: 19 | picante, 20 | colorspace, 21 | ggplot2, 22 | stringr, 23 | EBImage, 24 | reshape2, 25 | dichromat 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jo-fai Chow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2 (4.0.1): do not edit by hand 2 | 3 | export(create_ggtheme) 4 | export(display_colours) 5 | export(extract_colours) 6 | export(interpolate_colours) 7 | export(mpd_select_colours) 8 | export(palette_reduce) 9 | export(plot_radar) 10 | export(simulate_colours) 11 | import(EBImage) 12 | import(colorspace) 13 | import(dichromat) 14 | import(ggplot2) 15 | import(reshape2) 16 | import(stringr) 17 | importFrom(picante,mpd) 18 | -------------------------------------------------------------------------------- /R/create_ggtheme.R: -------------------------------------------------------------------------------- 1 | #' Create customised themes for ggplot2 object 2 | #' 3 | #' This function creates a ggplot2 theme object based on my favourite templates. 4 | #' 5 | #' @param theme The name of the temaplate (blank, xkcd, more to come ...) 6 | #' 7 | #' @examples 8 | #' theme_blank <- create_ggtheme("blank") 9 | #' theme_xkcd <- create_ggtheme("xkcd") 10 | #' @export 11 | #' @import ggplot2 stringr 12 | 13 | 14 | create_ggtheme <- function(theme = "blank") { 15 | 16 | if (theme == "xkcd") { 17 | 18 | output_theme <- theme(panel.background = element_rect(fill="white"), 19 | axis.ticks = element_line(colour=NA), 20 | panel.grid = element_line(colour="white"), 21 | axis.text.y = element_text(colour=NA), 22 | axis.text.x = element_text(colour="black"), 23 | text = element_text(size=16, family="Humor Sans")) 24 | 25 | } else if (theme == "blank") { 26 | 27 | output_theme <- theme_bw() 28 | output_theme$line <- element_blank() 29 | output_theme$rect <- element_blank() 30 | output_theme$strip.text <- element_blank() 31 | output_theme$axis.text <- element_blank() 32 | output_theme$plot.title <- element_blank() 33 | output_theme$axis.title <- element_blank() 34 | output_theme$plot.margin <- structure(c(0, 0, -1, -1), 35 | unit = "lines", 36 | valid.unit = 3L, 37 | class = "unit") 38 | 39 | } 40 | 41 | ## Return 42 | return(output_theme) 43 | 44 | } -------------------------------------------------------------------------------- /R/display_colours.R: -------------------------------------------------------------------------------- 1 | #' Display colour using rblocks 2 | #' 3 | #' This function display colours using rblocks 4 | #' 5 | #' @param inp_col Colours input as names or hex code 6 | #' 7 | #' @examples 8 | #' set.seed(1234) 9 | #' pal_pf <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 10 | #' display_colours(pal_pf) 11 | #' @export 12 | 13 | display_colours <- function(inp_col) { 14 | 15 | ## Make block 16 | block <- make_block(data.frame(matrix(inp_col, nrow = 1))) 17 | 18 | ## Update Colours 19 | block[,] <- inp_col 20 | 21 | ## Update Column Names 22 | colnames(block) <- inp_col 23 | 24 | ## Display 25 | rblocks::display(block) 26 | 27 | } 28 | -------------------------------------------------------------------------------- /R/extract_colours.R: -------------------------------------------------------------------------------- 1 | #' Detect and extract dominant colours from an image 2 | #' 3 | #' This function downloads an image and extracts the dominant colours from it. 4 | #' The code is based on dsparks' gist (https://gist.github.com/dsparks/3980277). 5 | #' 6 | #' @param url_img URL of the image (can be PNG, JPG, JPEG, TIFF) or a local file. Can also 7 | #' be an \code{EBImage} "Image" object 8 | #' @param num_col Number of dominant colours to be extracted (default = 5) 9 | #' @param rsize Size to reduce original image to, for further processing. This is the size, in pixels, 10 | #' for the largest dimension, from which colours are extracted. Please be careful with this option, as setting it 11 | #' too high can result in an extremely large processing time (or even running out of physical memory). 12 | #' 13 | #' @examples 14 | #' #Example 1a - Using the R logo to create a 5-colour palette 15 | #' pal_r <- extract_colours("http://developer.r-project.org/Logo/Rlogo-1.png") 16 | #' par(mfrow = c(1,2)) 17 | #' pie(rep(1, 5), col = pal_r, main = "Palette based on R Logo") 18 | #' hist(Nile, breaks = 5, col = pal_r, main = "Palette based on R Logo") 19 | #' 20 | #' #Example 1b - Using the Kill Bill poster to create a 5-colour palette 21 | #' pal_kb <- extract_colours("http://www.moviegoods.com/Assets/product_images/1010/477803.1010.A.jpg") 22 | #' par(mfrow = c(1,2)) 23 | #' pie(rep(1, 5), col = pal_kb, main = "Palette based on Kill Bill") 24 | #' hist(Nile, breaks = 5, col = pal_kb, main = "Palette based on Kill Bill") 25 | #' 26 | #' #Example 1c - Using Homer Simpson 27 | #' pal_s <- extract_colours("http://haphappy.com/wp-content/uploads/2011/03/homerbeer2.png") 28 | #' par(mfrow = c(1,2)) 29 | #' pie(rep(1, 5), col = pal_s, main = "Palette based on Simpsons") 30 | #' hist(Nile, breaks = 5, col = pal_s, main = "Palette based on Simpsons") 31 | #' @export 32 | #' @import EBImage reshape2 33 | 34 | extract_colours <- function( 35 | url_img = "http://developer.r-project.org/Logo/Rlogo-1.png", num_col = 5, rsize = 100) { 36 | 37 | ## Read Image 38 | if (class(url_img) != "Image") { 39 | img <- readImage(url_img) # local file or url 40 | } else { 41 | img <- url_img # is already a loaded "Image" 42 | } 43 | 44 | ## Resize Image (make it smaller so the remaining tasks run faster) 45 | if (max(dim(img)[1:2]) > rsize) { 46 | if (dim(img)[1] > dim(img)[2]) { 47 | img <- resize(img, w = rsize) 48 | } else { 49 | img <- resize(img, h = rsize) 50 | } 51 | } 52 | 53 | ## Melt 54 | img_melt <- melt(img) 55 | 56 | ## Reshape 57 | img_rgb <- reshape(img_melt, timevar = "Var3", idvar = c("Var1", "Var2"), direction = "wide") 58 | img_rgb$Var1 <- -img_rgb$Var1 59 | 60 | ## Detect dominant colours with kmeans (multiple starts) 61 | col_dom <- kmeans(img_rgb[, 3:5], centers = num_col, nstart = 3, iter.max = 100) 62 | 63 | ## Return k-means centers as RGB colours 64 | cus_pal <- sort(rgb(col_dom$centers)) 65 | return(as.character(cus_pal)) 66 | 67 | } 68 | -------------------------------------------------------------------------------- /R/interpolate_colours.R: -------------------------------------------------------------------------------- 1 | #' Interpolate colour palette to render more colours within the input range 2 | #' 3 | #' This function extend a colour palette by interpolation. 4 | #' 5 | #' @param inp_col Colours input as names or hex code 6 | #' @param n_col Number of output colours 7 | #' 8 | #' @examples 9 | #' set.seed(1234) 10 | #' inp_col <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 11 | #' out_col <- interpolate_colours(inp_col, n_col = 20) 12 | #' display_colours(out_col) 13 | #' @export 14 | 15 | interpolate_colours <- function(inp_col, n_col) { 16 | 17 | out_col <- colorRampPalette(inp_col, interpolate = 'spline')(n_col) 18 | 19 | return(out_col) 20 | 21 | } 22 | -------------------------------------------------------------------------------- /R/mpd_select_colours.R: -------------------------------------------------------------------------------- 1 | #' Select colours from a palette to maximize perceptual distance between the colours 2 | #' 3 | #' This function takes a palette as a character vector of hexidecimal colours, and returns a smaller palette, 4 | #' attempting to maximize the mean perceptual distance (MPD) between the colours in the new palette. It uses CIELAB 5 | #' colorspace to map colour to a perceptually meaningful scale before maximizing distances 6 | #' 7 | #' @param pal A palette as a character vector containing hexidecimal coded colours 8 | #' @param sat.thresh Minimum saturation of colours in resulting palette (between 0 and 1) 9 | #' @param light.thresh Maximum luminosity of colours in resulting palette (between 0 and 1) 10 | #' @param dark.thresh Minimum luminosity of colours in resulting palette (between 0 and 1) 11 | #' @param nreps The number of samples of the new colour palette to perform for brute force optimization of MPD 12 | #' @param ncolours The number of colours to select for the new palette 13 | #' @param nreturn The number of palettes to return 14 | #' @return If \code{nreturn} > 1 then a list of length \code{nreturn}, each element of which is a character vector of length ncolours consisting of hexidecimal colour codes, 15 | #' else a character vector of length ncolours consisting of hexidecimal colour codes 16 | #' @importFrom picante mpd 17 | #' @import colorspace 18 | #' @export 19 | mpd_select_colours <- function(pal, sat.thresh = NULL, light.thresh = NULL, dark.thresh = NULL, nreps = 10000, ncolours = ceiling(length(pal)/2), nreturn = 1) { 20 | if (ncolours > length(pal)) { 21 | stop("Number of colours to select must be less than the total number of colours in the palette") 22 | } 23 | if (ncolours < 2) { 24 | stop("You must select at least two colours") 25 | } 26 | ## get rid of undesirable colours 27 | pal_red <- palette_reduce(pal, sat.thresh = sat.thresh, light.thresh = light.thresh, dark.thresh = dark.thresh) 28 | if (length(pal_red) == 0) { 29 | stop("No colours after thresholding") 30 | } 31 | if (length(pal_red) < 2) { 32 | stop("Too few colours after thresholding") 33 | } 34 | ## convert hex colours to RGB space 35 | cols <- hex2RGB(pal_red, gamma = TRUE) 36 | rownames(cols@coords) <- pal_red 37 | ## convert colours to CIELAB colorspace 38 | cols.LAB <- coords(as(cols, "LAB")) 39 | ## generate distances between all colours in perceptual space 40 | col_dists <- dist(cols.LAB) 41 | ## randomly sample nreps palettes of ncolours 42 | col_samp <- t(replicate(nreps, as.numeric((rownames(cols.LAB) %in% sample(rownames(cols.LAB),ncolours))))) 43 | colnames(col_samp) <- rownames(cols.LAB) 44 | ## calculate MPD: Mean Phylogenetic Distance, which happens to have the same acronym as Mean Perceptual Distance :) 45 | col_mpd <- mpd(col_samp, as.matrix(col_dists)) 46 | ## take indices of only unique values of MPD 47 | col_unique <- which(duplicated(col_mpd)==FALSE) 48 | ## return top nreturn palettes in a list 49 | if (nreturn > length(col_unique)) { 50 | warning("There were fewer than nreturn palettes generated; Returning all palettes") 51 | new_pals_index <- apply(col_samp[col_unique,], 1, function(x) x==1) 52 | new_pals <- apply(new_pals_index, 2, function(x) rownames(new_pals_index)[x]) 53 | new_pals <- lapply(seq_len(ncol(new_pals)), function(x) new_pals[,x]) 54 | new_pals <- new_pals[order(col_unique)] 55 | } else { 56 | col_unique <- col_unique[rank(-col_mpd[col_unique]) <= nreturn] 57 | if (nreturn > 1) { 58 | new_pals_index <- apply(col_samp[col_unique,], 1, function(x) x==1) 59 | new_pals <- apply(new_pals_index, 2, function(x) rownames(new_pals_index)[x]) 60 | new_pals <- lapply(seq_len(ncol(new_pals)), function(x) new_pals[,x]) 61 | new_pals <- new_pals[order(col_unique)] 62 | } else { 63 | new_pals_index <- col_samp[col_unique,]==1 64 | new_pals <- names(new_pals_index)[new_pals_index] 65 | } 66 | } 67 | return(new_pals) 68 | } 69 | -------------------------------------------------------------------------------- /R/palette_reduce.R: -------------------------------------------------------------------------------- 1 | #' Reduce palette by removing colours with low saturation, or low or high luminance 2 | #' @param pal A palette as a character vector containing hexidecimal coded colours 3 | #' @param sat.thresh Minimum saturation of colours in resulting palette (between 0 and 1) 4 | #' @param light.thresh Maximum luminosity of colours in resulting palette (between 0 and 1) 5 | #' @param dark.thresh Minimum luminosity of colours in resulting palette (between 0 and 1) 6 | #' @export 7 | #' @import colorspace 8 | palette_reduce <- function(pal, sat.thresh = NULL, light.thresh = NULL, dark.thresh = NULL) { 9 | cols <- hex2RGB(pal, gamma = TRUE) 10 | if (!is.null(sat.thresh)) { 11 | pal <- pal[coords(as(cols, "HLS"))[,3] > sat.thresh] 12 | cols <- hex2RGB(pal, gamma = TRUE) 13 | } 14 | if (!is.null(light.thresh)) { 15 | ## convert light thresholds from proportions into L*A*B* scale 16 | light.thresh <- ceiling((1-light.thresh)*100) 17 | pal <- pal[coords(as(cols, "LAB"))[,1] < light.thresh] 18 | cols <- hex2RGB(pal, gamma = TRUE) 19 | } 20 | ## get rid of dark colours if desired 21 | if (!is.null(dark.thresh)) { 22 | ## convert dark thresholds from proportions into L*A*B* scale (perceptual) 23 | dark.thresh <- ceiling(dark.thresh*100) 24 | pal <- pal[coords(as(cols, "LAB"))[,1] > dark.thresh] 25 | } 26 | pal 27 | } -------------------------------------------------------------------------------- /R/plot_radar.R: -------------------------------------------------------------------------------- 1 | #' Create radar (spider) charts from a data frame 2 | #' 3 | #' This function creates a radar chart. 4 | #' Original Code: http://statisticstoproveanything.blogspot.co.uk/2013/11/spider-web-plots-in-r.html 5 | #' @export 6 | 7 | plot_radar <- function(data, data.row = NULL, y.cols = NULL, main = NULL, add = F, 8 | col = "red", lty = 1, scale = T) { 9 | 10 | if (!is.matrix(data) & !is.data.frame(data)) 11 | stop("Requires matrix or data.frame") 12 | if (is.null(y.cols)) 13 | y.cols = colnames(data)[sapply(data, is.numeric)] 14 | if (sum(!sapply(data[, y.cols], is.numeric)) > 0) { 15 | out = paste0("\"", colnames(data)[!sapply(data, is.numeric)], "\"", 16 | collapse = ", ") 17 | stop(paste0("All y.cols must be numeric\n", out, " are not numeric")) 18 | } 19 | if (is.null(data.row)) 20 | data.row = 1 21 | if (is.character(data.row)) 22 | if (data.row %in% rownames(data)) { 23 | data.row = which(rownames(data) == data.row) 24 | } else { 25 | stop("Invalid value for data.row:\nMust be a valid rownames(data) or row-index value") 26 | } 27 | if (is.null(main)) 28 | main = rownames(data)[data.row] 29 | if (scale == T) { 30 | data = scale(data[, y.cols]) 31 | data = apply(data, 2, function(x) x/max(abs(x))) 32 | } 33 | data = as.data.frame(data) 34 | n.y = length(y.cols) 35 | min.rad = 360/n.y 36 | polar.vals = (90 + seq(0, 360, length.out = n.y + 1)) * pi/180 37 | 38 | # 39 | if (add == F) { 40 | plot(0, xlim = c(-2.2, 2.2), ylim = c(-2.2, 2.2), type = "n", axes = F, 41 | xlab = "", ylab = "") 42 | title(main) 43 | lapply(polar.vals, function(x) lines(c(0, 2 * cos(x)), c(0, 2 * sin(x)))) 44 | lapply(1:n.y, function(x) text(2.15 * cos(polar.vals[x]), 2.15 * sin(polar.vals[x]), 45 | y.cols[x], cex = 0.8)) 46 | 47 | lapply(seq(0.5, 2, 0.5), function(x) lines(x * cos(seq(0, 2 * pi, length.out = 100)), 48 | x * sin(seq(0, 2 * pi, length.out = 100)), lwd = 0.5, lty = 2, col = "gray60")) 49 | lines(cos(seq(0, 2 * pi, length.out = 100)), sin(seq(0, 2 * pi, length.out = 100)), 50 | lwd = 1.2, col = "gray50") 51 | } 52 | 53 | 54 | r = 1 + data[data.row, y.cols] 55 | xs = r * cos(polar.vals) 56 | ys = r * sin(polar.vals) 57 | xs = c(xs, xs[1]) 58 | ys = c(ys, ys[1]) 59 | 60 | lines(xs, ys, col = col, lwd = 2, lty = lty) 61 | 62 | } 63 | -------------------------------------------------------------------------------- /R/simulate_colours.R: -------------------------------------------------------------------------------- 1 | #' Simulate Three Different Types of Colour Blindness (Deuteranopia, Protanopia & Tritanopia) 2 | #' 3 | #' This function simulates three different types of colour blindness (http://www.color-blindness.com/2010/03/09/types-of-color-blindness/). 4 | #' 5 | #' @param inp_col Original Colours (Hex Code or Colour Names) 6 | #' 7 | #' @examples 8 | #' set.seed(1234) 9 | #' pal_pf <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 10 | #' simulate_colours(pal_pf) 11 | #' @export 12 | #' @import dichromat 13 | 14 | simulate_colours <- function(inp_col) { 15 | 16 | ## Simulate three types of colour blindness 17 | col_deu <- dichromat(inp_col, type = "deutan") 18 | col_pro <- dichromat(inp_col, type = "protan") 19 | col_tri <- dichromat(inp_col, type = "tritan") 20 | 21 | ## Create rblocks 22 | block <- make_block(data.frame(matrix(NA, nrow = length(inp_col), ncol = 4))) 23 | 24 | ## Update colours 25 | block[, 1] <- inp_col 26 | block[, 2] <- col_deu 27 | block[, 3] <- col_pro 28 | block[, 4] <- col_tri 29 | 30 | ## Rename columns 31 | colnames(block) <- c("Input", "Deutan", "Protan", "Tritan") 32 | 33 | ## Display 34 | rblocks::display(block) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rPlotter 2 | ======== 3 | 4 | A collection of wrapper functions for graphics. The objective of this package is to make plotting easier for R beginners. 5 | 6 | ## Dependencies 7 | 8 | This package depends on the following packages: 9 | - ggplot2 (available on CRAN) 10 | - stringr (available on CRAN) 11 | - reshape2 (available on CRAN) 12 | - dichromat (available on CRAN) 13 | - picante (available on CRAN) 14 | - colorspace (available on CRAN) 15 | - EBImage (available on Bioconductor) 16 | - rblocks (available on GitHub) 17 | 18 | ## Installation 19 | 20 | ``` 21 | ## CRAN Packages 22 | install.packages(c("ggplot2", "stringr", "reshape2", "dichromat")) 23 | 24 | ## EBImage 25 | source("http://bioconductor.org/biocLite.R") 26 | biocLite("EBImage") 27 | 28 | ## Packages on GitHub 29 | library(devtools) 30 | install_github("ramnathv/rblocks") 31 | 32 | ## And finally ... 33 | install_github("woobe/rPlotter") 34 | ``` 35 | 36 | ## Example: extract_colours(...) 37 | 38 | This function extracts dominant colours from images and then returns colour hex code. 39 | 40 | ``` 41 | library(rPlotter) 42 | ``` 43 | ``` 44 | ## Using the R Logo 45 | pal_r <- extract_colours("http://developer.r-project.org/Logo/Rlogo-1.png") 46 | par(mfrow = c(1,2)) 47 | pie(rep(1, 5), col = pal_r, main = "Palette based on R Logo") 48 | hist(Nile, breaks = 5, col = pal_r, main = "Palette based on R Logo") 49 | ``` 50 | 51 | ![output_1a](http://i.imgur.com/41Q40Hk.png) 52 | 53 | ``` 54 | ## Using a poster from the movie "Kill Bill" 55 | pal_kb <- extract_colours("http://www.moviegoods.com/Assets/product_images/1010/477803.1010.A.jpg") 56 | par(mfrow = c(1,2)) 57 | pie(rep(1, 5), col = pal_kb, main = "Palette based on Kill Bill") 58 | hist(Nile, breaks = 5, col = pal_kb, main = "Palette based on Kill Bill") 59 | ``` 60 | 61 | ![output_1b](http://i.imgur.com/XUqOTSk.png) 62 | 63 | ``` 64 | ## Using Homer Simpson 65 | pal_s <- extract_colours("http://haphappy.com/wp-content/uploads/2011/03/homerbeer2.png") 66 | par(mfrow = c(1,2)) 67 | pie(rep(1, 5), col = pal_s, main = "Palette based on Simpsons") 68 | hist(Nile, breaks = 5, col = pal_s, main = "Palette based on Simpsons") 69 | ``` 70 | 71 | ![output_1c](http://i.imgur.com/BiNAO9H.png) 72 | 73 | 74 | ## Example: display_colours(...) 75 | 76 | This function displays colours as rblocks. 77 | 78 | ``` 79 | set.seed(1234) 80 | pal_pf <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 81 | display_colours(pal_pf) 82 | ``` 83 | 84 | ![output_disp](http://i.imgur.com/tpsealV.png) 85 | 86 | ## Example: simulate_colours(...) 87 | 88 | The functions simulates three types of colour blindness and then displays the simulated colours as rblocks. 89 | 90 | ``` 91 | set.seed(1234) 92 | pal_pf <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 93 | simulate_colours(pal_pf) 94 | ``` 95 | 96 | ![output_sim](http://i.imgur.com/xhQsb5y.png) 97 | 98 | ## Related Blog Posts 99 | 100 | - [Towards (Yet) Another R Colour Palette Generator (27/05/2014)](http://bit.ly/bib_colour1) 101 | 102 | ![blog_1a](http://i.imgur.com/YwCy6lZ.png) 103 | 104 | 105 | ## Credits 106 | 107 | - Original K-means Palette by dsparks https://gist.github.com/dsparks/3980277 108 | - [Karthik Ram](https://github.com/karthik) for the original [wesanderson](https://github.com/karthik/wesanderson) palette generator. 109 | - [Russell Dinnage](https://github.com/rdinnager) and [Noam Ross](https://github.com/noamross) for new ideas and suggestions. 110 | - [Ramnath Vaidyanathan](https://github.com/ramnathv) for [rblocks](https://github.com/ramnathv/rblocks). 111 | - 'xkcd' theme from http://drunks-and-lampposts.com/2012/10/02/clegg-vs-pleb-an-xkcd-esque-chart/ 112 | - 'blank' theme from http://is-r.tumblr.com/post/32728434389/a-replacement-for-theme-blank 113 | - Radar chart from http://statisticstoproveanything.blogspot.co.uk/2013/11/spider-web-plots-in-r.html 114 | -------------------------------------------------------------------------------- /man/create_ggtheme.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{create_ggtheme} 3 | \alias{create_ggtheme} 4 | \title{Create customised themes for ggplot2 object} 5 | \usage{ 6 | create_ggtheme(theme = "blank") 7 | } 8 | \arguments{ 9 | \item{theme}{The name of the temaplate (blank, xkcd, more to come ...)} 10 | } 11 | \description{ 12 | This function creates a ggplot2 theme object based on my favourite templates. 13 | } 14 | \examples{ 15 | theme_blank <- create_ggtheme("blank") 16 | theme_xkcd <- create_ggtheme("xkcd") 17 | } 18 | 19 | -------------------------------------------------------------------------------- /man/display_colours.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{display_colours} 3 | \alias{display_colours} 4 | \title{Display colour using rblocks} 5 | \usage{ 6 | display_colours(inp_col) 7 | } 8 | \arguments{ 9 | \item{inp_col}{Colours input as names or hex code} 10 | } 11 | \description{ 12 | This function display colours using rblocks 13 | } 14 | \examples{ 15 | set.seed(1234) 16 | pal_pf <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 17 | display_colours(pal_pf) 18 | } 19 | 20 | -------------------------------------------------------------------------------- /man/extract_colours.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{extract_colours} 3 | \alias{extract_colours} 4 | \title{Detect and extract dominant colours from an image} 5 | \usage{ 6 | extract_colours(url_img = "http://developer.r-project.org/Logo/Rlogo-1.png", 7 | num_col = 5, rsize = 100) 8 | } 9 | \arguments{ 10 | \item{url_img}{URL of the image (can be PNG, JPG, JPEG, TIFF) or a local file. Can also 11 | be an \code{EBImage} "Image" object} 12 | 13 | \item{num_col}{Number of dominant colours to be extracted (default = 5)} 14 | 15 | \item{rsize}{Size to reduce original image to, for further processing. This is the size, in pixels, 16 | for the largest dimension, from which colours are extracted. Please be careful with this option, as setting it 17 | too high can result in an extremely large processing time (or even running out of physical memory).} 18 | } 19 | \description{ 20 | This function downloads an image and extracts the dominant colours from it. 21 | The code is based on dsparks' gist (https://gist.github.com/dsparks/3980277). 22 | } 23 | \examples{ 24 | #Example 1a - Using the R logo to create a 5-colour palette 25 | pal_r <- extract_colours("http://developer.r-project.org/Logo/Rlogo-1.png") 26 | par(mfrow = c(1,2)) 27 | pie(rep(1, 5), col = pal_r, main = "Palette based on R Logo") 28 | hist(Nile, breaks = 5, col = pal_r, main = "Palette based on R Logo") 29 | 30 | #Example 1b - Using the Kill Bill poster to create a 5-colour palette 31 | pal_kb <- extract_colours("http://www.moviegoods.com/Assets/product_images/1010/477803.1010.A.jpg") 32 | par(mfrow = c(1,2)) 33 | pie(rep(1, 5), col = pal_kb, main = "Palette based on Kill Bill") 34 | hist(Nile, breaks = 5, col = pal_kb, main = "Palette based on Kill Bill") 35 | 36 | #Example 1c - Using Homer Simpson 37 | pal_s <- extract_colours("http://haphappy.com/wp-content/uploads/2011/03/homerbeer2.png") 38 | par(mfrow = c(1,2)) 39 | pie(rep(1, 5), col = pal_s, main = "Palette based on Simpsons") 40 | hist(Nile, breaks = 5, col = pal_s, main = "Palette based on Simpsons") 41 | } 42 | 43 | -------------------------------------------------------------------------------- /man/interpolate_colours.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{interpolate_colours} 3 | \alias{interpolate_colours} 4 | \title{Interpolate colour palette to render more colours within the input range} 5 | \usage{ 6 | interpolate_colours(inp_col, n_col) 7 | } 8 | \arguments{ 9 | \item{inp_col}{Colours input as names or hex code} 10 | 11 | \item{n_col}{Number of output colours} 12 | } 13 | \description{ 14 | This function extend a colour palette by interpolation. 15 | } 16 | \examples{ 17 | set.seed(1234) 18 | inp_col <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 19 | out_col <- interpolate_colours(inp_col, n_col = 20) 20 | display_colours(out_col) 21 | } 22 | 23 | -------------------------------------------------------------------------------- /man/mpd_select_colours.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{mpd_select_colours} 3 | \alias{mpd_select_colours} 4 | \title{Select colours from a palette to maximize perceptual distance between the colours} 5 | \usage{ 6 | mpd_select_colours(pal, sat.thresh = NULL, light.thresh = NULL, 7 | dark.thresh = NULL, nreps = 10000, ncolours = ceiling(length(pal)/2), 8 | nreturn = 1) 9 | } 10 | \arguments{ 11 | \item{pal}{A palette as a character vector containing hexidecimal coded colours} 12 | 13 | \item{sat.thresh}{Minimum saturation of colours in resulting palette (between 0 and 1)} 14 | 15 | \item{light.thresh}{Maximum luminosity of colours in resulting palette (between 0 and 1)} 16 | 17 | \item{dark.thresh}{Minimum luminosity of colours in resulting palette (between 0 and 1)} 18 | 19 | \item{nreps}{The number of samples of the new colour palette to perform for brute force optimization of MPD} 20 | 21 | \item{ncolours}{The number of colours to select for the new palette} 22 | 23 | \item{nreturn}{The number of palettes to return} 24 | } 25 | \value{ 26 | If \code{nreturn} > 1 then a list of length \code{nreturn}, each element of which is a character vector of length ncolours consisting of hexidecimal colour codes, 27 | else a character vector of length ncolours consisting of hexidecimal colour codes 28 | } 29 | \description{ 30 | This function takes a palette as a character vector of hexidecimal colours, and returns a smaller palette, 31 | attempting to maximize the mean perceptual distance (MPD) between the colours in the new palette. It uses CIELAB 32 | colorspace to map colour to a perceptually meaningful scale before maximizing distances 33 | } 34 | 35 | -------------------------------------------------------------------------------- /man/palette_reduce.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{palette_reduce} 3 | \alias{palette_reduce} 4 | \title{Reduce palette by removing colours with low saturation, or low or high luminance} 5 | \usage{ 6 | palette_reduce(pal, sat.thresh = NULL, light.thresh = NULL, 7 | dark.thresh = NULL) 8 | } 9 | \arguments{ 10 | \item{pal}{A palette as a character vector containing hexidecimal coded colours} 11 | 12 | \item{sat.thresh}{Minimum saturation of colours in resulting palette (between 0 and 1)} 13 | 14 | \item{light.thresh}{Maximum luminosity of colours in resulting palette (between 0 and 1)} 15 | 16 | \item{dark.thresh}{Minimum luminosity of colours in resulting palette (between 0 and 1)} 17 | } 18 | \description{ 19 | Reduce palette by removing colours with low saturation, or low or high luminance 20 | } 21 | 22 | -------------------------------------------------------------------------------- /man/plot_radar.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{plot_radar} 3 | \alias{plot_radar} 4 | \title{Create radar (spider) charts from a data frame} 5 | \usage{ 6 | plot_radar(data, data.row = NULL, y.cols = NULL, main = NULL, add = F, 7 | col = "red", lty = 1, scale = T) 8 | } 9 | \description{ 10 | This function creates a radar chart. 11 | Original Code: http://statisticstoproveanything.blogspot.co.uk/2013/11/spider-web-plots-in-r.html 12 | } 13 | 14 | -------------------------------------------------------------------------------- /man/simulate_colours.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2 (4.0.1): do not edit by hand 2 | \name{simulate_colours} 3 | \alias{simulate_colours} 4 | \title{Simulate Three Different Types of Colour Blindness (Deuteranopia, Protanopia & Tritanopia)} 5 | \usage{ 6 | simulate_colours(inp_col) 7 | } 8 | \arguments{ 9 | \item{inp_col}{Original Colours (Hex Code or Colour Names)} 10 | } 11 | \description{ 12 | This function simulates three different types of colour blindness (http://www.color-blindness.com/2010/03/09/types-of-color-blindness/). 13 | } 14 | \examples{ 15 | set.seed(1234) 16 | pal_pf <- extract_colours("http://www.scoutlondon.com/blog/wp-content/uploads/2012/05/Pulp-Fiction.jpg") 17 | simulate_colours(pal_pf) 18 | } 19 | 20 | -------------------------------------------------------------------------------- /rPlotter.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | PackageRoxygenize: rd,collate,namespace 19 | --------------------------------------------------------------------------------